Just getting started - but stuck alerady

Irish Rover

New member
Local time
Today, 14:06
Joined
Dec 3, 2012
Messages
3
Greetings,

I hato to ask such a basic question to such an esteemed group, but I am new at this and stuck alerady. Can someone point out why this simple exercise will not run. I keep getting "Syntax Error.

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

Thank You!

Irish Rover
 
Lose the underscores at the ends of the lines.

Code:
Sub GuessName()
    Msg = "Is Your Name " & Application.UserName & "?"
    Ans = MsgBox(Msg, vbYesNo)
    If Ans = vbNo Then 
        MsgBox "Oh, Never Mind."
    Else
        MsgBox "I must be psychic!"
    End If
End Sub
 
Thanks Steve, That works great.
I have another question; How come when I write this in the following fashion, it does not work?

Sub GuessName()
Msg = "Is Your Name " & Application.UserName & "?"
Ans = MsgBox(Msg, vbYesNo)
If Ans = vbNo Then MsgBox "Oh Never Mind." Else MsgBox "I Must be Psychic!"
End If
End Sub

Thanks

Irish Rover
 
Because your IF block is wrong

You can either have an 'in-line' IF block with no Else condition (generally regarded as bad practice BTW)

If blah blah Then do something

or you can have a 'block IF' construct.

If blah blah Then
do something
Else
do something else
End If
 
Thank You for your assistance. I found out that the author of the Excel for dummies book actually missed the fact that the page this was written on did not format out correctly - so he says. He has not gotten to the proper construct for these IF blocks - Yet. Hope I understand it then.

Irish Rover
 

Users who are viewing this thread

Back
Top Bottom