|
-
Nov 18th, 2022, 11:27 PM
#1
Thread Starter
Addicted Member
Error that makes absolutely no sense - please help!
I have been programming for a ‘few years’ and feel that I can generally get things done. However, I just ran into a problem in Excel VBA that makes literally no sense to me. Here is the code (simplified to just show the issue):
Code:
Sub testme()
Dim i, j As Integer
For i = 1 To 10
Do
While j > 5
Next i ‘ ** this line
End Sub
When I click Debug->Compile VBA Project, it shows: ‘Compile error: Next without For’ on the line indicated.
This makes less than no sense as the code is perfectly reasonable. What the heck is going on??? More importantly, how can I use this trivial construct in my code and make it work?
-
Nov 18th, 2022, 11:45 PM
#2
Re: Error that makes absolutely no sense - please help!
Should be:
Code:
Do
Loop While (Condition)
-
Nov 19th, 2022, 10:58 AM
#3
Re: Error that makes absolutely no sense - please help!
Just to add some clarity to what is happening here, a standalone "While" statement is not valid syntax to close a Do loop. A standalone "While" statement is actually the beginning statement of a While-Wend loop.
So, properly indenting your code to reflect what is actually happening, this is effectively what you have:
Code:
Sub testme()
Dim i, j As Integer
For i = 1 To 10
Do
While j > 5
Next i ‘ ** this line
End Sub
So, from this, it should be clear why a Next without For statement is encountered.
-
Nov 20th, 2022, 06:34 PM
#4
Thread Starter
Addicted Member
Re: Error that makes absolutely no sense - please help!
doh!!! I forgot Loop - thanks!
-
Nov 20th, 2022, 09:41 PM
#5
Re: Error that makes absolutely no sense - please help!
If your issue has been resolved, please use the Thread Tools menu to mark the thread Resolved, so we can see that you need no more help without opening the thread and reading it.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|