Test if cells are selected
Lisa,
Try this. It will print into the immediate window which row in column D
(Column 4) is selected.
Dim c As Object
For Each c In Selection
If c.Column = 4 Then
'Do someting
Debug.Print "Row: " & c.Row
End If
Next
Please rate this response when you get a change.
Good Luck
--
Stewart Rogers
DataSort Software, L.C.
"Lisa" wrote:
I have a VSTO Excel app where I need to test each row in a range to
see whether the user has selected the cell in column D in that row,
and carry out an operation if they have.
But I'm getting some very erratic results. I tried using For Each
cell as Cell in ActiveWorksheet.Selection.Cells, but that didn't
work. So I went to this:
Dim _col As Integer
Dim _row As Integer
For cellCount As Integer = 0 To .Application.Selection.cells.count -
1
_col = .Application.Selection.cells(cellCount).Column
_row = .Application.Selection.cells(cellCount).Row + 1
The "+ 1" on the row makes no sense to me, but it was off by a row if
I didn't do that.
The problem is, when I select multiple cells in column D, I get weird
results. Two contiguous ones works fine. But if I pick four
contiguous ones, it only sees the first and third. Actually, it
depends on the order in which I select them. And if they aren't
contiguous, it gets even weirder.
Is there a simpler way to do this? A way where instead of going
through the selection and testing the cells to see if they're in the
right column, I can go down that column and check to see if each cell
is selected? I haven't been able to find a Selected or IsSelected
property on cells.
Thanks,
Lisa
|