Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Direction for looping through a Selection.
When looping through a selection that spans several rows
and columns, VBA evaluates all the cells in the first column of the selection and then procedes to the next column in the selection. Is it possible to loop through a selection by evaluating all the rows first? For example, you have the following selection, A1:D10. Using a For Next loop all, cells A1:A10 will be evaluated first followed by B1:B10, then C1:C10, etc. I want to be able to evalute the cells in A1:D1 first, followed by A2:D2, etc. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Direction for looping through a Selection.
Bob,
By default, Excel goes through rows first, then columns. If you want to explicitly control the order of movement, here's some example code: Sub TryNow() Dim myCell As Range Dim i As Integer Dim j As Long Dim myRange As Range Set myRange = Range("A1:B2") 'Default For Each myCell In myRange MsgBox myCell.Address Next myCell 'ColumnWise For i = 1 To myRange.Columns.Count For j = 1 To myRange.Rows.Count MsgBox myRange.Cells(j, i).Address Next j Next i 'RowWise For j = 1 To myRange.Rows.Count For i = 1 To myRange.Columns.Count MsgBox myRange.Cells(j, i).Address Next i Next j End Sub HTH, Bernie Excel MVP "Bob J." wrote in message ... When looping through a selection that spans several rows and columns, VBA evaluates all the cells in the first column of the selection and then procedes to the next column in the selection. Is it possible to loop through a selection by evaluating all the rows first? For example, you have the following selection, A1:D10. Using a For Next loop all, cells A1:A10 will be evaluated first followed by B1:B10, then C1:C10, etc. I want to be able to evalute the cells in A1:D1 first, followed by A2:D2, etc. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Direction for looping through a Selection.
Sub Tester2()
Dim sStr As String For Each rw In Selection.Rows For Each cell In rw.Cells sStr = sStr & cell.Address(0, 0) & ", " Next sStr = Left(sStr, Len(sStr) - 2) & vbNewLine Next MsgBox sStr End Sub -- Regards, Tom Ogilvy "Bob J." wrote in message ... When looping through a selection that spans several rows and columns, VBA evaluates all the cells in the first column of the selection and then procedes to the next column in the selection. Is it possible to loop through a selection by evaluating all the rows first? For example, you have the following selection, A1:D10. Using a For Next loop all, cells A1:A10 will be evaluated first followed by B1:B10, then C1:C10, etc. I want to be able to evalute the cells in A1:D1 first, followed by A2:D2, etc. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Limiting selection in a cell AND linking that selection to a list | Excel Discussion (Misc queries) | |||
how do I move selection after enter direction | Excel Discussion (Misc queries) | |||
Copy Selection - Transpose Selection - Delete Selection | Excel Discussion (Misc queries) | |||
Looping a selection of rows | Excel Discussion (Misc queries) | |||
In Excel 2000, can I change the direction of the move selection a. | Excel Discussion (Misc queries) |