0

Can anyone tell me why I would be getting a compile error: "Loop without do" for the following code.

Sub Burrito_log()

    Dim j As Long
    j = 1
    Do
        x = InputBox("How many burritos did you have today?")
        Cells(j, 2) = x
        Cells(j, 1) = Date
        j = j + 1

        ans = MsgBox("Burrito Log", vbYesNo, "Are you done inputting?")
        If ans = vbYes Then
            'Do nothing
        Else
            Loop
        End If

End Sub
2
  • 1
    Alexandre gave you the answer. I would highlight, also, that your Loop doesn't have a break clause. Be aware that if you never Exit your Do you will eat Burritos forever. Commented Mar 28, 2015 at 16:43
  • You have to close your If block before you can close your loop. Commented Mar 28, 2015 at 17:05

3 Answers 3

1

After formating your code the error is obvious. Your Loop is located before your End If. Just move it after End If.

Sign up to request clarification or add additional context in comments.

Comments

1

Reading your code, it seems you want to loop only if the answer is "No". The syntax you use is incorrect, you need to nest an Exit Do breaking clause in the If block but you cannot nest the Loop keyword inside. Here's how it should be:

Sub Burrito_log()

    Dim j As Long
    j = 1
    Do
        x = InputBox("How many burritos did you have today?")
        Cells(j, 2) = x
        Cells(j, 1) = Date
        j = j + 1

        ans = MsgBox("Burrito Log", vbYesNo, "Are you done inputting?")
        If ans = vbYes Then
            Exit Do
        End If
    Loop
End Sub

Alternatively, you can avoid the If block by using the While keyword of the Do Loop:

Do While ans <> vbYes
    'no If block --> Exit Do needed
Loop

Comments

0

Change to:

ans = MsgBox("Burrito Log", vbYesNo, "Are you done inputting?") 

If ans = vbYes Then Exit Do
Loop

I think this is what you were trying to do, but you had the syntax off a bit.

I would add more explanation, but I'm on my phone.

There's more info on loop syntax here http://www.excel-easy.com/vba/loop.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.