Compile Eror: Next without for

49 Views Asked by At

I'm just trying to learn my code, and here it is:

Sub lala()
Dim ha As String
Dim objcell As Object
Dim ha2 As String
Dim ha3 As String
    For Each objcell In ActiveSheet
    MsgBox ("BLAH")
    ha = MsgBox("Do you want the same message box?", vbYesNo)
    If ha = vbYes Then
    Next objcell
    Else
    MsgBox("Do you want another one at all?", vbYesNo) = ha3
    If ha3 = vbYes Then
    ha2 = InputBox("What do you want it to say?")
    Else
    Exit Sub
    End If
    End If
End Sub
1

There are 1 best solutions below

3
Veve On

You are mixing an "if" and a "for", your "next" is in the "if" statement, while it should be outside it. Here is your "corrected" code :

Sub lala()
    Dim ha As String
    Dim objcell As Object
    Dim ha2 As String
    Dim ha3 As String
    For Each objcell In ActiveSheet
        MsgBox ("BLAH")
        ha = MsgBox("Do you want the same message box?", vbYesNo)
        If ha = vbNo Then Exit for
    Next objcell
    MsgBox("Do you want another one at all?", vbYesNo) = ha3
    If ha3 = vbYes Then
        ha2 = InputBox("What do you want it to say?")
    Else
        Exit Sub
    End If
End Sub