1

I have been trying to fix it but was not able to, although it's a very small code: Please help, it's showing an "else without if" error:

Private Sub Workbook_Open()

Line2:
    Dim pass As String
    pass = InputBox("Enter Password")
    If pass = "hummer" Then GoTo Line1
    Else
    GoTo Line2
    End If

Line1:
    End Sub

2 Answers 2

2

You can do this more concisely and avoid using GOTO to control the flow of the program (which is generally a good idea) with a simple loop;

Dim pass As String

Do
    pass = InputBox("Enter Password")
    If pass = "hummer" Then Exit Do
Loop
Sign up to request clarification or add additional context in comments.

1 Comment

Certainly the simplest way there is.
0

The problem is you used a one liner IF statement.
Try this:

    If pass = "hummer" Then 
        GoTo Line1
    Else
        GoTo Line2
    End If

There are 3 ways to contruct IF statement.
One is the One Liner Construct as I've mentioned above, which does not require to be terminated with End If.

Example:

If pass = "hummer" Then GoTo Line1 Else GoTo Line2

Second is the End If Terminated Construct. Example of which is what I posted above.
Third one is the If-EsleIf-Else End If terminated constuct if you have multiple conditions.

Example:

If pass = "hummer" Then 
    GoTo Line1 
ElseIf pass = "something_else" Then
    GoTo Line2
Else
    GoTo Line3
End If

Hope this helps.

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.