Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi,
I wonder if anyone can explain the following. When I have a very simple for next loop, for example: for i=1 to worksheets.count sheets(i).select activesheet.name="a" & i next i the first time the macro goes through the loop, when you would expect the value of i to be 1, the value of i is the highest value, i.e. worksheets.count. By the way, it does not seem to be consistent. If I stop and restart the macro, it seems to go through the loop correctly i.e with i starting from 1. Can anybody explain how this works to me? Thanks! |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
When the loop runs, i will always start at 1. I suspect that you have stopped the macro or a bug has occurred after the loop has run. If you then look at the value of i it will show you its last executed value, which at that point will be worksheets.count. regards Paul On Nov 28, 11:30*am, wrote: Hi, I wonder if anyone can explain the following. When I have a very simple for next loop, for example: for i=1 to worksheets.count sheets(i).select activesheet.name="a" & i next i the first time the macro goes through the loop, when you would expect the value of i to be 1, the value of i is the highest value, i.e. worksheets.count. By the way, it does not seem to be consistent. If I stop and restart the macro, it seems to go through the loop correctly i.e with i starting from 1. Can anybody explain how this works to me? Thanks! |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try this. You dont have to selet the sheet to rename it.
Sub reNameSheets() Dim ws As Worksheet Dim i As Long For Each ws In Worksheets i = i + 1 ws.Name = "a" & i Next ws End Sub " wrote: Hi, I wonder if anyone can explain the following. When I have a very simple for next loop, for example: for i=1 to worksheets.count sheets(i).select activesheet.name="a" & i next i the first time the macro goes through the loop, when you would expect the value of i to be 1, the value of i is the highest value, i.e. worksheets.count. By the way, it does not seem to be consistent. If I stop and restart the macro, it seems to go through the loop correctly i.e with i starting from 1. Can anybody explain how this works to me? Thanks! |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Or use your own code like this
Sub reName() For i = 1 To Worksheets.Count Sheets(i).Name = "a" & i Next i End Sub " wrote: Hi, I wonder if anyone can explain the following. When I have a very simple for next loop, for example: for i=1 to worksheets.count sheets(i).select activesheet.name="a" & i next i the first time the macro goes through the loop, when you would expect the value of i to be 1, the value of i is the highest value, i.e. worksheets.count. By the way, it does not seem to be consistent. If I stop and restart the macro, it seems to go through the loop correctly i.e with i starting from 1. Can anybody explain how this works to me? Thanks! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Excel and its Visual Basic IDE | Excel Discussion (Misc queries) | |||
Programmatic access to visual basic project via Visual FoxPro | Excel Programming | |||
Can I run Visual Basic procedure using Excel Visual Basic editor? | Excel Programming | |||
changing the visual basic in office 2003 to visual studio net | Excel Discussion (Misc queries) |