#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 422
Default Nested Loop

Even though the below code can't duplicate sheetname after the first 5 j
loops, Why when I step through the code after the 5th time through the j
Does it go back to For j (and when I hold the mouse over j it shows 6
And want to run lines 4 and 5 a sixth time?
Confused with Nested looping here..



Sub AddSheets()
For i = 1 To 52
For j = 1 To 5
Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Format(Weekday(j + 1), "dddd")
Next j
Next i
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Nested Loop

I suspect you are just seeing the value of J when it is in the outer loop:

Sub AddSheets()
For i = 1 To 3
Debug.Print i, j
For j = 1 To 5
Debug.Print "--" & j
Next j
Next i
End Sub

produces:

1
--1
--2
--3
--4
--5
2 6
--1
--2
--3
--4
--5
3 6
--1
--2
--3
--4
--5


--
Regards,
Tom Ogilvy



"JMay" wrote in message
...
Even though the below code can't duplicate sheetname after the first 5 j
loops, Why when I step through the code after the 5th time through the j
Does it go back to For j (and when I hold the mouse over j it shows 6
And want to run lines 4 and 5 a sixth time?
Confused with Nested looping here..



Sub AddSheets()
For i = 1 To 52
For j = 1 To 5
Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Format(Weekday(j + 1), "dddd")
Next j
Next i
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default Nested Loop

<And want to run lines 4 and 5 a sixth time?

It doesn't. You see the value 6, but it will not execute the next line; it will revert to 1 just in time.
Of course you get errors because of duplicate sheetnames then.

--
Kind regards,

Niek Otten
Microsoft MVP - Excel

"JMay" wrote in message ...
| Even though the below code can't duplicate sheetname after the first 5 j
| loops, Why when I step through the code after the 5th time through the j
| Does it go back to For j (and when I hold the mouse over j it shows 6
| And want to run lines 4 and 5 a sixth time?
| Confused with Nested looping here..
|
|
|
| Sub AddSheets()
| For i = 1 To 52
| For j = 1 To 5
| Sheets.Add After:=Worksheets(Worksheets.Count)
| ActiveSheet.Name = Format(Weekday(j + 1), "dddd")
| Next j
| Next i
| End Sub
|


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 422
Default Nested Loop

I promise, when stepping through my code I monitored the j value and as
it went through the loop with the j value of 5 in lines 4 and 5 it went
to
Line 6 (did nothing) then moved to line 7 (Next I - and did nothing)
then,
When I pressed F8 it jumped back to For j = 1 to 5 where j showed as = 6
- I did an F8 it then Jumped to line 4 << Ilooked ahead a line 5 and j
= 1;
Sorry, but if anything can go wrong -- I will find it - or it will find
me, grrrrr


"JMay" wrote in message ...
| Even though the below code can't duplicate sheetname after the first 5 j
| loops, Why when I step through the code after the 5th time through the j
| Does it go back to For j (and when I hold the mouse over j it shows 6
| And want to run lines 4 and 5 a sixth time?
| Confused with Nested looping here..
|
|
|
| Sub AddSheets()
| For i = 1 To 52
| For j = 1 To 5
| Sheets.Add After:=Worksheets(Worksheets.Count)
| ActiveSheet.Name = Format(Weekday(j + 1), "dddd")
| Next j
| Next i
| End Sub
|


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Nested Loop

The value of j isn't changed until the for command is executed. The loop
isn't exited until j 5 (6). when the for statement is highlighted, it
hasn't executed.

So the only thing going wrong is misinterpreting what you observe.

--
Regards,
Tom Ogilvy

"JMay" wrote in message
...
I promise, when stepping through my code I monitored the j value and as it
went through the loop with the j value of 5 in lines 4 and 5 it went to
Line 6 (did nothing) then moved to line 7 (Next I - and did nothing) then,
When I pressed F8 it jumped back to For j = 1 to 5 where j showed as = 6 -
I did an F8 it then Jumped to line 4 << Ilooked ahead a line 5 and j = 1;
Sorry, but if anything can go wrong -- I will find it - or it will find
me, grrrrr


"JMay" wrote in message
...
| Even though the below code can't duplicate sheetname after the first 5
j
| loops, Why when I step through the code after the 5th time through the
j
| Does it go back to For j (and when I hold the mouse over j it shows 6
| And want to run lines 4 and 5 a sixth time?
| Confused with Nested looping here..
|
|
|
| Sub AddSheets()
| For i = 1 To 52
| For j = 1 To 5
| Sheets.Add After:=Worksheets(Worksheets.Count)
| ActiveSheet.Name = Format(Weekday(j + 1), "dddd")
| Next j
| Next i
| End Sub
|




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
Nested loop [email protected] Excel Worksheet Functions 1 April 5th 07 12:50 AM
Nested Loop Link ashcrusher Excel Worksheet Functions 0 January 13th 06 03:46 AM
nested if loop with logical OR/AND sa02000[_9_] Excel Programming 4 October 11th 05 03:44 PM
Nested loop order? jclark419[_4_] Excel Programming 2 July 29th 05 07:41 PM
Nested Loop Frank Kabel Excel Programming 6 September 12th 04 02:41 AM


All times are GMT +1. The time now is 03:47 AM.

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

About Us

"It's about Microsoft Excel"