I have this code (from Mike McGrath):-
Sub Main()
Dim i As Integer
Dim j As Integer
Dim Pass As Integer
For i = 1 To 3
For j = 1 To 3
Pass = Pass + 1
Next j
Next i
End Sub
If I step through the code I see that there are 30 steps.
I want to know when:-
“you reach the third outer Next statement”
- please.
I believe it to be step 21 of 30 which is:-
Next i
(After the above step 21 of 30 gets run i gets set to 3).
Is my thinking correct?
Thanks in advance for any replies.
When do you reach the, "third outer Next statement"?
-
- NewLounger
- Posts: 4
- Joined: 31 May 2020, 21:09
-
- Administrator
- Posts: 78235
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: When do you reach the, "third outer Next statement"?
I see only two Next statements, and the Pass = Pass + 1 line is executed only 3*3 = 9 times. What am I missing?
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 4
- Joined: 31 May 2020, 21:09
Re: When do you reach the, "third outer Next statement"?
Hello Hans.
Thanks for the reply.
Here are the 30 steps:-
Sub Main()
Dim i As Integer
Dim j As Integer
Dim Pass As Integer
For i = 1 To 3
For j = 1 To 3
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Next i
For j = 1 To 3
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Next i
For j = 1 To 3
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Next i
End Sub
I don’t think you’re missing anything.
This statement:-
Pass = Pass + 1 line is executed only 3*3 = 9 times
- is correct.
My question remains the same.
Thanks in advance for any replies.
Thanks for the reply.
Here are the 30 steps:-
Sub Main()
Dim i As Integer
Dim j As Integer
Dim Pass As Integer
For i = 1 To 3
For j = 1 To 3
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Next i
For j = 1 To 3
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Next i
For j = 1 To 3
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Pass = Pass + 1
Next j
Next i
End Sub
I don’t think you’re missing anything.
This statement:-
Pass = Pass + 1 line is executed only 3*3 = 9 times
- is correct.
My question remains the same.
Thanks in advance for any replies.
-
- Administrator
- Posts: 78235
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: When do you reach the, "third outer Next statement"?
I'm sorry, this cannot be correct - you have three Next j's after each other, so the code won't even start.
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 4
- Joined: 31 May 2020, 21:09
Re: When do you reach the, "third outer Next statement"?
Code runs perfectly OK here Hans as per McGrath's example (I can let you have the file and my analysis of it if necessary).
Not a big problem though if my question can't be answered.
As far as I can see my thinking as outlined in my first posting's correct.
McGrath's example is only intended to show the debugging process so, perhaps, it doesn't make sense from others' point of view.
Thanks for the help.
Not a big problem though if my question can't be answered.
As far as I can see my thinking as outlined in my first posting's correct.
McGrath's example is only intended to show the debugging process so, perhaps, it doesn't make sense from others' point of view.
Thanks for the help.
-
- Administrator
- Posts: 78235
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: When do you reach the, "third outer Next statement"?
The code in your first post is syntactically correct, but it has only two For ... Next loops - there is no "third outer Next statement".
It is impossible that the code from your reply will run, I suspect that you copy/pasted something incorrectly.
It is impossible that the code from your reply will run, I suspect that you copy/pasted something incorrectly.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 78235
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: When do you reach the, "third outer Next statement"?
Could you attach a sample workbook with the code?
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 4
- Joined: 31 May 2020, 21:09
Re: When do you reach the, "third outer Next statement"?
Here's the file (see attachment).
Note that when I say it works it means that it works by stepping through it which is all that the author, to the best of my knowledge, intended.
Note that when I say it works it means that it works by stepping through it which is all that the author, to the best of my knowledge, intended.
You do not have the required permissions to view the files attached to this post.
Last edited by trip_to_tokyo on 30 Apr 2022, 12:56, edited 1 time in total.
-
- Administrator
- Posts: 78235
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: When do you reach the, "third outer Next statement"?
Thanks, I think I understand what you mean now. You're asking "when do you reach the outer Next statement for the third time".
And the text in your second post is not the macro, as I thought, but the lines executed as you single-step through the code.
When I do this, the following lines are executed:
The three lines beginning with Dim aren't executed. The Visual Basic interpreter compiles them before running the code, so execution doesn't stop at them when you single-step.
The outer Next statement Next i is executed three times, as the 10th, 18th and 26th statement. So the third time is #26, just before the end.
At least, that's how I see it...
And the text in your second post is not the macro, as I thought, but the lines executed as you single-step through the code.
When I do this, the following lines are executed:
The three lines beginning with Dim aren't executed. The Visual Basic interpreter compiles them before running the code, so execution doesn't stop at them when you single-step.
The outer Next statement Next i is executed three times, as the 10th, 18th and 26th statement. So the third time is #26, just before the end.
At least, that's how I see it...
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 4
- Joined: 31 May 2020, 21:09
Re: When do you reach the, "third outer Next statement"?
The three lines beginning with Dim aren't executed. YES HANS YOU'RE QUITE CORRECT BUT I INCLUDED THEM FOR THE SAKE OF COMPLETENESS / CLARITY / EASE OF COMPREHENSION / EASE OF UNDERSTANDING THE EXACT LOGIC OF THE STEPS.
I've run over my steps again and can see (thank you) from where you get your numbers 10th, 18th and 26th statements (my 13, 21, 29).
I was (from what you say) incorrectly counting the first For i = 1 To 3 as the first, "outer Next statement" because that's where i got set to 1.
Thanks for that Hans; all clear now.
I've run over my steps again and can see (thank you) from where you get your numbers 10th, 18th and 26th statements (my 13, 21, 29).
I was (from what you say) incorrectly counting the first For i = 1 To 3 as the first, "outer Next statement" because that's where i got set to 1.
Thanks for that Hans; all clear now.
-
- 2StarLounger
- Posts: 142
- Joined: 11 Jun 2012, 20:37
Re: When do you reach the, "third outer Next statement"?
I want to know when:-
“you reach the third outer Next statement”
You can add a line to the code (Debug.Assert) which will stop the code when you reach the 3rd outer Next statement:
The Debug line is executed at each outer loop and checks that i <> 3 in which case it does nothing, but when i become 3 it can no longer confirm (assert) that i <> 3 so stops the code, highlighting the debug line, after which you can step through the code with F8.
“you reach the third outer Next statement”
You can add a line to the code (Debug.Assert) which will stop the code when you reach the 3rd outer Next statement:
Code: Select all
Sub Main()
Dim i As Integer
Dim j As Integer
Dim Pass As Integer
For i = 1 To 3
Debug.Assert i <> 3
For j = 1 To 3
Pass = Pass + 1
Next j
Next i
End Sub
You do not have the required permissions to view the files attached to this post.