Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error with MsgBox

Tags:

excel

vba

This results in a syntax error:

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

Why?

like image 414
Bitterblue Avatar asked Feb 09 '26 23:02

Bitterblue


1 Answers

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.

like image 106
Jon Egerton Avatar answered Feb 13 '26 17:02

Jon Egerton