Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Test if cells are selected
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Test if cells are selected
In VBA, I'd use something like:
with selection if intersect(.cells, .parent.range("d:d")) is nothing then 'no cells in column D else 'some cells in column d end if end with 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 -- Dave Peterson |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Test if cells are selected
Thanks, but this doesn't address my problem.
Lisa On Mar 30, 11:18 am, Datasort wrote: 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- Hide quoted text - - Show quoted text - |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Test if cells are selected
Let me be more specific. I have a VSTO app using an Excel template.
I select four cells (G12, H14, H20 and J15, in this example), and run this code: For i As Integer = 0 To BudgetUpdate.Globals.shBudgetUpdate.Application.Se lection.Cells.Count - 1 MsgBox(BudgetUpdate.Globals.shBudgetUpdate.Applica tion.Selection.Cells(i).Address) Next I get four MsgBox results popping up, reading, in order: $G$11, $G$12, $G$13, $G$14. It's consistent, too. If I select G8, E14, G15 and I16, it returns $E $13, $E$14, $E$15, $E$16. Every time, it takes the leftmost column, and starts with the cell just above the one I selected in that column. And it seems to think that all of the rest of the selected cells are immediately beneath it. Is this a known bug? Is there a workaround? Lisa |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Test if cells are selected
Lisa,
A selection with non-contiguous sections has "Areas" - if you loop through these it might help. for each a in selection.areas for i=1 to a.cells.count msgbox a.cells(i).address next i next a -- Tim Williams Palo Alto, CA "Lisa" wrote in message oups.com... Let me be more specific. I have a VSTO app using an Excel template. I select four cells (G12, H14, H20 and J15, in this example), and run this code: For i As Integer = 0 To BudgetUpdate.Globals.shBudgetUpdate.Application.Se lection.Cells.Count - 1 MsgBox(BudgetUpdate.Globals.shBudgetUpdate.Applica tion.Selection.Cells(i).Address) Next I get four MsgBox results popping up, reading, in order: $G$11, $G$12, $G$13, $G$14. It's consistent, too. If I select G8, E14, G15 and I16, it returns $E $13, $E$14, $E$15, $E$16. Every time, it takes the leftmost column, and starts with the cell just above the one I selected in that column. And it seems to think that all of the rest of the selected cells are immediately beneath it. Is this a known bug? Is there a workaround? Lisa |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Test if cells are selected
I don't see this as a bug. .cells(#) doesn't cycle through the cells in the
range. It uses the first cell and then offsets from there. If you want to cycle through the range: dim myRng as range dim mycell as range set myrng = activesheet.range("g12,h14,h20,j15") for each mycell in myrng.cells msgbox mycell.address next mycell ====== or you could loop through the addresses: dim iCtr as long dim myAddr as variant myaddr = array("g12","h14","h20","J15") for ictr = lbound(myaddr) to ubound(myaddr) msgbox activesheet.range(myaddr(ictr)).address next ictr Lisa wrote: Let me be more specific. I have a VSTO app using an Excel template. I select four cells (G12, H14, H20 and J15, in this example), and run this code: For i As Integer = 0 To BudgetUpdate.Globals.shBudgetUpdate.Application.Se lection.Cells.Count - 1 MsgBox(BudgetUpdate.Globals.shBudgetUpdate.Applica tion.Selection.Cells(i).Address) Next I get four MsgBox results popping up, reading, in order: $G$11, $G$12, $G$13, $G$14. It's consistent, too. If I select G8, E14, G15 and I16, it returns $E $13, $E$14, $E$15, $E$16. Every time, it takes the leftmost column, and starts with the cell just above the one I selected in that column. And it seems to think that all of the rest of the selected cells are immediately beneath it. Is this a known bug? Is there a workaround? Lisa -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Multiple cells or columns are selected instead of selected cell or | Excel Discussion (Misc queries) | |||
Test to see if selected range is single row? | Excel Programming | |||
Cells are selected but aren't displayed as selected | Excel Worksheet Functions | |||
Macro to take selected cells times a selected cell | Excel Programming | |||
How can I test when any worksheet within a workbook is selected | Excel Programming |