ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can a For statement work in reverse (from 40 to 1) (https://www.excelbanter.com/excel-programming/404475-can-statement-work-reverse-40-1-a.html)

PCLIVE

Can a For statement work in reverse (from 40 to 1)
 
Is there a way to use a For statement in reverse?
For example:

For i = Sheets.Count To 4
Sheets(i).Activate
Next i

Instead of starting at sheet 4 and working up to the number of sheets in the
workbook, I'd like to start on the last sheet of the workbook and work back
to sheet 4. Is this possible?

The reason I want to do this is because based on certain criteria, I may be
deleting that sheet. Once that happens the sheet numbers get thrown off and
I end up missing some sheets and then possibly getting an error near the end
of the code when it tries to view sheets that don't exist.

Thanks in advance.
Paul


--




John Bundy

Can a For statement work in reverse (from 40 to 1)
 
For i = 4 To Sheets.Count step-1

--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"PCLIVE" wrote:

Is there a way to use a For statement in reverse?
For example:

For i = Sheets.Count To 4
Sheets(i).Activate
Next i

Instead of starting at sheet 4 and working up to the number of sheets in the
workbook, I'd like to start on the last sheet of the workbook and work back
to sheet 4. Is this possible?

The reason I want to do this is because based on certain criteria, I may be
deleting that sheet. Once that happens the sheet numbers get thrown off and
I end up missing some sheets and then possibly getting an error near the end
of the code when it tries to view sheets that don't exist.

Thanks in advance.
Paul


--





PCLIVE

Can a For statement work in reverse (from 40 to 1)
 
Thanks John. That's good to know for future use. However, I ended up using
a DoWhile loop and subtracting 1 each time. The outcome is the same, but
I'm sure using "step-1" would look better. I may end up reworking my code
with this.

Thanks again.
Paul

--

"John Bundy" (remove) wrote in message
...
For i = 4 To Sheets.Count step-1

--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"PCLIVE" wrote:

Is there a way to use a For statement in reverse?
For example:

For i = Sheets.Count To 4
Sheets(i).Activate
Next i

Instead of starting at sheet 4 and working up to the number of sheets in
the
workbook, I'd like to start on the last sheet of the workbook and work
back
to sheet 4. Is this possible?

The reason I want to do this is because based on certain criteria, I may
be
deleting that sheet. Once that happens the sheet numbers get thrown off
and
I end up missing some sheets and then possibly getting an error near the
end
of the code when it tries to view sheets that don't exist.

Thanks in advance.
Paul


--








All times are GMT +1. The time now is 04:00 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com