Forum Discussion

enqi.angel.xu's avatar
enqi.angel.xu
Copper Contributor
Mar 31, 2018
Solved

Complie error Expected: end of statement

Hi Everyone,

 

I just stared learning Excel VBA and I have copied the following code from a VBA book to VBE but it displayed the message: Complie error Expected: end of statement.

 

Sub GuessName()
Msg = “Is your name “ & Application.UserName & “?”
Ans = MsgBox(Msg, vbYesNo)
If Ans = vbNo Then MsgBox “Oh, never mind.”
If Ans = vbYes Then MsgBox “I must be psychic!”
End Sub

 

Would anymore knows where it is wrong and how can I fix the issue?

 

Really appreciated for your time!

 

  • I don't see anything wrong with your syntax.  Guessing you had some other code in this module that was causing your issue:

     

    Sub GuessName()
    Msg = "Is your name " & Application.UserName & "?"
    Ans = MsgBox(Msg, vbYesNo)
        If Ans = vbNo Then MsgBox "Oh, never mind."
        If Ans = vbYes Then MsgBox "I must be psychic!"
    End Sub
    

    If your new to VBA, then maybe it will be helpful to know the different arguments associated with MsgBox.  Some of the arguments like Button and Title can make your VBA solution more polished looking and allows for greater customization.

     

    Check the MsgBox arguments out here:

     

    https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/msgbox-function

     

     

     

     

3 Replies

  • Matt Mickle's avatar
    Matt Mickle
    Bronze Contributor

    I don't see anything wrong with your syntax.  Guessing you had some other code in this module that was causing your issue:

     

    Sub GuessName()
    Msg = "Is your name " & Application.UserName & "?"
    Ans = MsgBox(Msg, vbYesNo)
        If Ans = vbNo Then MsgBox "Oh, never mind."
        If Ans = vbYes Then MsgBox "I must be psychic!"
    End Sub
    

    If your new to VBA, then maybe it will be helpful to know the different arguments associated with MsgBox.  Some of the arguments like Button and Title can make your VBA solution more polished looking and allows for greater customization.

     

    Check the MsgBox arguments out here:

     

    https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/msgbox-function

     

     

     

     

    • enqi.angel.xu's avatar
      enqi.angel.xu
      Copper Contributor
      Hi Matt

      Thank you so much for your time and tips! I figured out later the issue was caused by the format of "", as I have copied the code from a textbook. Thank you for sending me the link of MsgBox arguments!

Resources