3

This results in a syntax error:

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

Why?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • 1
    possible duplicate of [Calling a Sub in VBA](http://stackoverflow.com/questions/7715044/calling-a-sub-in-vba) – Jean-François Corbett Jan 06 '14 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. – Bitterblue Jan 06 '14 at 15:10

1 Answers1

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.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129