Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Limiting selection in a cell AND linking that selection to a list Lisa Excel Discussion (Misc queries) 1 July 28th 09 05:00 PM
how do I move selection after enter direction plasiter Excel Discussion (Misc queries) 2 September 22nd 08 06:18 PM
Copy Selection - Transpose Selection - Delete Selection Uninvisible Excel Discussion (Misc queries) 2 October 23rd 07 04:18 PM
Looping a selection of rows Andre Kruger Excel Discussion (Misc queries) 1 December 15th 05 04:18 PM
In Excel 2000, can I change the direction of the move selection a. GTP Excel Discussion (Misc queries) 2 January 7th 05 01:20 AM


All times are GMT +1. The time now is 12:08 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"