Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default 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
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
Copy Selection - Transpose Selection - Delete Selection Uninvisible Excel Discussion (Misc queries) 2 October 23rd 07 04:18 PM
Loop and extend selection [email protected] Excel Programming 4 October 3rd 06 08:05 PM
Loop row selection copy with blank spaces Dave[_60_] Excel Programming 3 February 13th 06 04:33 PM
Using loop for selection macro akoobra[_2_] Excel Programming 1 January 28th 05 03:14 PM
Date selection loop Roger[_8_] Excel Programming 12 September 26th 04 12:53 AM


All times are GMT +1. The time now is 03:55 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"