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?
Re: Error that makes absolutely no sense - please help!
Should be:
Code:
Do
Loop While (Condition)
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.
Re: Error that makes absolutely no sense - please help!
doh!!! I forgot Loop - thanks!
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.