ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Index on a selection (https://www.excelbanter.com/excel-programming/370454-index-selection.html)

[KiO] ASamarcos

Index on a selection
 

Hi folks.

First time here, hope you can help me.

I'm trying to get a index on a for each next routine but I don't know
how to do it. Like the Item method. In the example I want the statement
to run on the last cell of the selection.

Example:

Sub Example()
For each cell in selection

if cell.index = selection.count then
statement
endif

next

end sub

Thanks for any help you can give.

Best regards,
KiO


--
[KiO] ASamarcos
------------------------------------------------------------------------
[KiO] ASamarcos's Profile: http://www.excelforum.com/member.php...o&userid=37554
View this thread: http://www.excelforum.com/showthread...hreadid=571874


Dave Peterson

Index on a selection
 
If it's a single area:

dim MyLastCell as range

with selection
set mylastcell = .cells(.cells.count)
end with

No need to loop through all the cell.

"[KiO] ASamarcos" wrote:

Hi folks.

First time here, hope you can help me.

I'm trying to get a index on a for each next routine but I don't know
how to do it. Like the Item method. In the example I want the statement
to run on the last cell of the selection.

Example:

Sub Example()
For each cell in selection

if cell.index = selection.count then
statement
endif

next

end sub

Thanks for any help you can give.

Best regards,
KiO

--
[KiO] ASamarcos
------------------------------------------------------------------------
[KiO] ASamarcos's Profile: http://www.excelforum.com/member.php...o&userid=37554
View this thread: http://www.excelforum.com/showthread...hreadid=571874


--

Dave Peterson

[KiO] ASamarcos[_2_]

Index on a selection
 

Hi Peterson.

Thanks for your help, but I actually want a way to see the index of the
cell that is being processed by the for each next routine.

Imagine that Selection.Count gives me 40. 40 cells in the range I
selected.

When the For Each starts I want to know, on each step, where the count
is. Is it 1, 2, 37?

I thought the Cell.Index could give me the answer but returns a error
message and I think theres a better way to do that than to put a
counter on it.

Best Regards,
KiO


--
[KiO] ASamarcos
------------------------------------------------------------------------
[KiO] ASamarcos's Profile: http://www.excelforum.com/member.php...o&userid=37554
View this thread: http://www.excelforum.com/showthread...hreadid=571874


Dave Peterson

Index on a selection
 
I don't think that there's a better way to do than to count the cells yourself.

But you do have a few choices.

You could do:

dim myRng as range
dim myCell as range
dim iCtr as long

ictr = 0
set myrng = range("c5:e12")
for each mycell in myrng.cells
ictr = ictr + 1
msgbox ictr & "--" & mycell.address(0,0)
next mycell

to see how the "pattern" that mycell takes.

=============

or you could loop through the columns of the range:

dim myRng as range
dim myCol as range
dim myCell as range
dim iCtr as long

ictr = 0
set myrng = range("c5:e12")
for each mycol in myrng.columns
for each mycell in mycol.cells
ictr = ictr + 1
msgbox ictr & "--" & mycell.address(0,0)
next mycell
next mycol

And see that pattern.

The same kind of thing to loop through each row.

"[KiO] ASamarcos" wrote:

Hi Peterson.

Thanks for your help, but I actually want a way to see the index of the
cell that is being processed by the for each next routine.

Imagine that Selection.Count gives me 40. 40 cells in the range I
selected.

When the For Each starts I want to know, on each step, where the count
is. Is it 1, 2, 37?

I thought the Cell.Index could give me the answer but returns a error
message and I think theres a better way to do that than to put a
counter on it.

Best Regards,
KiO

--
[KiO] ASamarcos
------------------------------------------------------------------------
[KiO] ASamarcos's Profile: http://www.excelforum.com/member.php...o&userid=37554
View this thread: http://www.excelforum.com/showthread...hreadid=571874


--

Dave Peterson


All times are GMT +1. The time now is 03:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com