VBA code shows “Else without if error”

前端 未结 2 973
别跟我提以往
别跟我提以往 2021-01-24 11:40

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         


        
2条回答
  •  孤街浪徒
    2021-01-24 12:08

    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.

提交回复
热议问题