Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Loop backwards through selection?
Hi,
Can someone please tell me how to loop from the last cell to the first in a selection? Thank you |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Loop backwards through selection?
Dim i As Long
Dim j As Long With Selection For i = .Rows.Count To 1 Step -1 For j = .Columns.Count To 1 Step -1 Debug.Print .Cells(i, j).Address, .Cells(i, j).Value Next j Next i End With -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "ppsa" wrote in message ... Hi, Can someone please tell me how to loop from the last cell to the first in a selection? Thank you |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Loop backwards through selection?
Hi,
One way, Loops backwards from A100 to A1 writing the numbers 100 to 1 in each cell Sub standard() For x = 100 To 1 Step -1 Cells(x, 1).Value = x Next End Sub Mike "ppsa" wrote: Hi, Can someone please tell me how to loop from the last cell to the first in a selection? Thank you |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Loop backwards through selection?
Thanks to both for the replies!
Bob, this works nicely; however, it does not work if the selection is non-contiguous. If there are non-adjacent cells in the selection (I'm selecting several rows, some of which are not adjacent to other rows in the selection), the number of rows returned is smaller than the number of rows in the selection! Do you know of any way around this problem? Thanks again. "Bob Phillips" wrote: Dim i As Long Dim j As Long With Selection For i = .Rows.Count To 1 Step -1 For j = .Columns.Count To 1 Step -1 Debug.Print .Cells(i, j).Address, .Cells(i, j).Value Next j Next i End With -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "ppsa" wrote in message ... Hi, Can someone please tell me how to loop from the last cell to the first in a selection? Thank you |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Loop backwards through selection?
For multiple areas (non-contiguous selections) you can adapt Bob's code by enclosing it in a loop through the Areas of the Selection. Sub AAA() Dim Area As Range Dim AreaNdx As Long Dim RowNdx As Long Dim ColNdx Dim N As Long For AreaNdx = Selection.Areas.Count To 1 Step -1 Set Area = Selection.Areas(AreaNdx) With Area For RowNdx = .Cells(.Cells.Count).Row To .Cells(1, 1).Row Step -1 For ColNdx = .Cells(.Cells.Count).Column To .Cells(1, 1).Column Step -1 N = N + 1 Cells(RowNdx, ColNdx) = N Next ColNdx Next RowNdx End With Next AreaNdx End Sub -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting www.cpearson.com (email on the web site) "ppsa" wrote in message ... Thanks to both for the replies! Bob, this works nicely; however, it does not work if the selection is non-contiguous. If there are non-adjacent cells in the selection (I'm selecting several rows, some of which are not adjacent to other rows in the selection), the number of rows returned is smaller than the number of rows in the selection! Do you know of any way around this problem? Thanks again. "Bob Phillips" wrote: Dim i As Long Dim j As Long With Selection For i = .Rows.Count To 1 Step -1 For j = .Columns.Count To 1 Step -1 Debug.Print .Cells(i, j).Address, .Cells(i, j).Value Next j Next i End With -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "ppsa" wrote in message ... Hi, Can someone please tell me how to loop from the last cell to the first in a selection? Thank you |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Loop backwards through selection?
Thank you. I think this issue is resolved now. :)
"Chip Pearson" wrote: For multiple areas (non-contiguous selections) you can adapt Bob's code by enclosing it in a loop through the Areas of the Selection. Sub AAA() Dim Area As Range Dim AreaNdx As Long Dim RowNdx As Long Dim ColNdx Dim N As Long For AreaNdx = Selection.Areas.Count To 1 Step -1 Set Area = Selection.Areas(AreaNdx) With Area For RowNdx = .Cells(.Cells.Count).Row To .Cells(1, 1).Row Step -1 For ColNdx = .Cells(.Cells.Count).Column To .Cells(1, 1).Column Step -1 N = N + 1 Cells(RowNdx, ColNdx) = N Next ColNdx Next RowNdx End With Next AreaNdx End Sub -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting www.cpearson.com (email on the web site) "ppsa" wrote in message ... Thanks to both for the replies! Bob, this works nicely; however, it does not work if the selection is non-contiguous. If there are non-adjacent cells in the selection (I'm selecting several rows, some of which are not adjacent to other rows in the selection), the number of rows returned is smaller than the number of rows in the selection! Do you know of any way around this problem? Thanks again. "Bob Phillips" wrote: Dim i As Long Dim j As Long With Selection For i = .Rows.Count To 1 Step -1 For j = .Columns.Count To 1 Step -1 Debug.Print .Cells(i, j).Address, .Cells(i, j).Value Next j Next i End With -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "ppsa" wrote in message ... Hi, Can someone please tell me how to loop from the last cell to the first in a selection? Thank you |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Copy Selection - Transpose Selection - Delete Selection | Excel Discussion (Misc queries) | |||
Loop and extend selection | Excel Programming | |||
Loop row selection copy with blank spaces | Excel Programming | |||
Using loop for selection macro | Excel Programming | |||
Date selection loop | Excel Programming |