3

This results in a syntax error:

Sub test()
    MsgBox("hello world", vbOKCancel) ' syntax error at this line
    Exit Sub
End Sub

Why?

2
  • 1
    possible duplicate of Calling a Sub in VBA Commented Jan 6, 2014 at 14:59
  • @Jean-FrançoisCorbett You should have come earlier. I mean the question is answered and the answer is a very good one. Way better than the one from the duplicate question. Commented Jan 6, 2014 at 15:10

1 Answer 1

10

You're just using the MsgBox method as a Sub. In VB6/VBA a Sub call either doesn't use brackets, or uses the Call keyword.

MsgBox "hello world", vbOKCancel

or

Call MsgBox("hello world", vbOKCancel) 

The brackets come into play when using the method as a function (ie you want the return value)

Dim msgResult

msgResult = MsgBox("hello world", vbOKCancel) 

I would guess that, since you're using vbOKCancel, this is the version you'll end up using to find out what the user clicked.

Sign up to request clarification or add additional context in comments.

3 Comments

The syntax of VBA makes me startle every time!
@mini-me: Yup - Its got plenty of "character"!
Parentheses, not brackets.

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.