0

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.

6
  • you are missing 1 x End If at the end of your code Commented Jun 6, 2017 at 4:45
  • Should there be 2 x End If ? I have already put one at the end Commented Jun 6, 2017 at 4:47
  • You need 2 x End If, one for the first If Hamza_Variable = 7 Then and a second because at the end you have another Else and not ElseIf, try adding a second one and see if it runs Commented Jun 6, 2017 at 4:48
  • I've tried adding it after the If Hamza_Variable = 7 Then and also at the end after the first End If. Still doesn't work Commented Jun 6, 2017 at 4:52
  • 1
    Did you mean ElseIf instead of Elself? (Note that you spelled the latter with an l instead of an I.) Commented Jun 6, 2017 at 4:52

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I had no idea about this. Will try it out and watch some tutorials on this too. thanks : )
@HamzaKhalid did you try it ?
0

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

6 Comments

I'm sorry I forgot to mention. I am ending with an End Sub. Still gives the same error
Paste the above code into excel. It works. I think you had a typo in the 'ElseIf'
Yes it was a typo, i just realized the stupid mistake which had me scratching my head since the last 3 hours. Thank you so much
How do I do that. Can't find the button
@HamzaKhalid I posted my comment as an answer, since you indicated that this was what resolved your issue. You may accept it by clicking the checkmark icon next to it once you are able to. Glad I could help!
|
0

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.)

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.