Index of Cells in a selection

R

Rod

Hello, everyone

I am trying to write code to determine the index of the "active" cell in a selection of say, two cells. Can you direct me to the way of learning how to go about that

I appreciate your time

Rod
 
L

Leo Heuser

Hello Rod

Here's one way:

Sub FindIndex()
'Leo Heuser, 14 Nov. 2003
Dim Cell As Range

For Each Cell In Selection.Cells
If Cell.Address = ActiveCell.Address Then
MsgBox "Index = " & Cell.Row - Selection.Row + 1 _
& ", " & Cell.Column - Selection.Column + 1
Exit For
End If
Next Cell

End Sub


--
Best Regards
Leo Heuser

Followup to newsgroup only please.

Rod said:
Hello, everyone!

I am trying to write code to determine the index of the "active" cell in a
selection of say, two cells. Can you direct me to the way of learning how
to go about that?
 
T

Tom Ogilvy

Ranges don't really have indexes. Are the cells contiguous?

What are you actually trying to do?

--
Regards,
Tom Ogilvy

Rod said:
Hello, everyone!

I am trying to write code to determine the index of the "active" cell in a
selection of say, two cells. Can you direct me to the way of learning how
to go about that?
 
T

Tom Ogilvy

First, this is no criticism of Leo who offered an excellent intepretation of
a vague question and offered an excellent suggesgted solution.

To the Original Poster,
Just curious. That gives you something like the offset from the first cell
in the range.

For example, select F8, then hold down the control key and select C3, so C3
is the active cell. Then run the macro. Is that what you were asking for?
 
L

Leo Heuser

Thanks, Tom, but I'm afraid, this is a case,
where I didn't see the forest for the trees :-(

Sub FindIndex()

MsgBox "Index = " & ActiveCell.Row - Selection.Row + 1 _
& ", " & ActiveCell.Column - Selection.Column + 1

End Sub

does exactly the same.

LeoH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top