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
  • 36,032
  • 27
  • 135
  • 183
Bitterblue
  • 11,254
  • 15
  • 80
  • 119
  • 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

9

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
  • 38,633
  • 11
  • 95
  • 128