Hamza Khalid
Hamza Khalid

Reputation: 241

Why am I getting an "End of Statement" compile error in my Excel VBA code?

Below is my code in VBA:

Sub Hamza_Starting_to_Learn()

Dim Hamza_Variable As Long

Hamza_Variable = 7

If Hamza_Variable = 7 Then

MsgBox Hamza_Variable & " Is da bomb"

Elself Hamza_Variable > 7 Then

MsgBox Hamza_Variable & " Is da bigger bomb"

Else

MsgBox Hamza_Variable & "We got ourselves a problem here bro"

End If

It is continuously giving the compile error "End of Statement" on the following line:

Elself Hamza_Variable > 7 Then

It highlights Then and gives this error.

Upvotes: 0

Views: 1520

Answers (3)

Knowledge Cube
Knowledge Cube

Reputation: 1010

In your original code, you wrote Elself instead of ElseIf (note the typo confusing l and I) in the line where you are getting your error. This likely confused VBA, which in turn gave you that misleading error message.

(Don't feel too bad. Other people have also had problems distinguishing these two letters in some monospace fonts before.)

Upvotes: 0

Shai Rado
Shai Rado

Reputation: 33682

Since you are starting to learn, maybe you should get familair with Select Case statement:

Option Explicit

Sub Hamza_Starting_to_Learn()

Dim Hamza_Variable As Long

Hamza_Variable = 7

Select Case Hamza_Variable
    Case 7
        MsgBox Hamza_Variable & " Is da bomb"
    Case Is > 7
        MsgBox Hamza_Variable & " Is da bigger bomb"
    Case Else
        MsgBox Hamza_Variable & "We got ourselves a problem here bro"

End Select

End Sub

Upvotes: 1

Pat Mustard
Pat Mustard

Reputation: 1902

Try this:

Sub Hamza_Starting_to_Learn()

    Dim Hamza_Variable As Long

        Hamza_Variable = 7

    If (Hamza_Variable = 7) Then

        MsgBox Hamza_Variable & " Is da bomb"

    ElseIf Hamza_Variable > 7 Then

        MsgBox Hamza_Variable & " Is da bigger bomb"

    Else

        MsgBox Hamza_Variable & "We got ourselves a problem here bro"

    End If

End Sub

You didn't end your sub routine with End Sub

Upvotes: 0

Related Questions