Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default How to find a cell's location/index within a range?

I'm trying to determine the location of a cell within a range. I know
that you can access cells in a range sequentially using
range(_).cells(1), .cells(2), etc. What I'd like to be able to do is
determine the 'i' for which range(_).cells(i) is the given cell. I can
write a function easily enough to do this, but for economy I was just
wondering if there was a trick I'd missed that would do it without.

The range is contiguous, but not necessarily rectangular, so I can't
simply multiple the row of the range that the cell is in by the column.

Anyone know if such an inverse for .cells(i) exists?

Thanks very much,

Chris

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default How to find a cell's location/index within a range?

I don't understand the question. cells() takes 2 arguments, not 1

consider the sub

Sub enumerate(R As Range)
Dim i As Long
For i = 1 To R.Cells.Count
R.Cells(i).Value = i
Next
End Sub

if you try it with a rectangular range:

Sub test()
enumerate Range("A1:C5")
End Sub

it *does* enumerate the range - though the fact that it does so seems
to be undocumented.

On the other hand, if you try

Sub test()
enumerate Range("A1:C5,B6:C10")
End Sub

Then you see that the non-contiguous range is *not* enumerated (A1:C8,
A9 is instead). So - what "i" are you looking for?

In general, if you want to access the cells sequentially, then use the
construct

for each cl in R.Cells

It should be possible to reverse-engineer the actual order in which
that enumeration is done - to find out when a given cell occurs it
looks like you would count the number of cells in the range strictly
above the given cell and add to it the number of cells in the range in
the row to the left of (and including) the given cell in that row. The
Intersect function would enter in (but handling a cell in row A would
require special care since there are no cells above it)

HTH

-John Coleman
On Jan 25, 3:43 am, wrote:
I'm trying to determine the location of a cell within a range. I know
that you can access cells in a range sequentially using
range(_).cells(1), .cells(2), etc. What I'd like to be able to do is
determine the 'i' for which range(_).cells(i) is the given cell. I can
write a function easily enough to do this, but for economy I was just
wondering if there was a trick I'd missed that would do it without.

The range is contiguous, but not necessarily rectangular, so I can't
simply multiple the row of the range that the cell is in by the column.

Anyone know if such an inverse for .cells(i) exists?

Thanks very much,

Chris


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default How to find a cell's location/index within a range?

Hi John,

Thanks for this. I do have a similar sort of procedure which loops
through the range and checks to see whether cell(i) matches the chosen
cell. This seems to work, but thanks to your example I can see I'm
going to run into problems with non-rectangular ranges. I'll bear that
in mind. Looping through cell objects, as you suggest, may work better.

Thanks for your help,

Chris

PS. The reason I knew about the single-argument usage of cells() was
through this introductory site:
http://www.anthony-vba.kefra.com/vba/vbabasic2.htm



On 25 Jan, 13:28, "John Coleman" wrote:
I don't understand the question. cells() takes 2 arguments, not 1

consider the sub

Sub enumerate(R As Range)
Dim i As Long
For i = 1 To R.Cells.Count
R.Cells(i).Value = i
Next
End Sub

if you try it with a rectangular range:

Sub test()
enumerate Range("A1:C5")
End Sub

it *does* enumerate the range - though the fact that it does so seems
to be undocumented.

On the other hand, if you try

Sub test()
enumerate Range("A1:C5,B6:C10")
End Sub

Then you see that the non-contiguous range is *not* enumerated (A1:C8,
A9 is instead). So - what "i" are you looking for?

In general, if you want to access the cells sequentially, then use the
construct

for each cl in R.Cells

It should be possible to reverse-engineer the actual order in which
that enumeration is done - to find out when a given cell occurs it
looks like you would count the number of cells in the range strictly
above the given cell and add to it the number of cells in the range in
the row to the left of (and including) the given cell in that row. The
Intersect function would enter in (but handling a cell in row A would
require special care since there are no cells above it)

HTH

-John Coleman
On Jan 25, 3:43 am, wrote:



I'm trying to determine the location of a cell within a range. I know
that you can access cells in a range sequentially using
range(_).cells(1), .cells(2), etc. What I'd like to be able to do is
determine the 'i' for which range(_).cells(i) is the given cell. I can
write a function easily enough to do this, but for economy I was just
wondering if there was a trick I'd missed that would do it without.


The range is contiguous, but not necessarily rectangular, so I can't
simply multiple the row of the range that the cell is in by the column.


Anyone know if such an inverse for .cells(i) exists?


Thanks very much,


Chris- Hide quoted text -- Show quoted text -


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using cell's contents as an index to a row ...? Mac Excel Worksheet Functions 3 February 16th 09 12:59 AM
Using cell's contents as an index to a row ... ? Mac Excel Worksheet Functions 1 February 14th 09 12:07 AM
Using cell's contents as an index to a row ... ? Mac Excel Worksheet Functions 0 February 13th 09 11:36 PM
how do i find and replace a portion of a cell's formula? KyWilde Excel Discussion (Misc queries) 2 November 3rd 06 02:49 AM
how to find last row/col index within a selected range Jeff Excel Programming 2 October 6th 04 03:58 PM


All times are GMT +1. The time now is 08:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"