Results 1 to 5 of 5

Thread: Error that makes absolutely no sense - please help!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    Question 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?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Error that makes absolutely no sense - please help!

    Should be:

    Code:
    Do
    Loop While (Condition)

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    Re: Error that makes absolutely no sense - please help!

    doh!!! I forgot Loop - thanks!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width