Microsoft Office Tutorials and References
In Depth Information
The VBA MsgBox Function
Table 12-2: Constants Used for Buttons in the Msgbox Function
Constant
Value
Description
vbOKOnly
0
Display OK button only.
vbOKCancel
1
Display OK and Cancel buttons.
vbAbortRetryIgnore
2
Display Abort, Retry, and Ignore buttons.
vbYesNoCancel
3
Display Yes, No, and Cancel buttons.
vbYesNo
4
Display Yes and No buttons.
vbRetryCancel
5
Display Retry and Cancel buttons.
vbCritical
16
Display Critical Message icon.
vbQuestion
32
Display Warning Query icon.
vbExclamation
48
Display Warning Message icon.
vbInformation
64
Display Information Message icon.
vbDefaultButton1
0
First button is default.
vbDefaultButton2
256
Second button is default.
vbDefaultButton3
512
Third button is default.
vbDefaultButton4
768
Fourth button is default.
vbSystemModal
4096
All applications are suspended until the user responds to the
message box (might not work under all conditions).
vbMsgBoxHelpButton
16384
Display a Help button. However, there is no way to display
any help if the button is clicked.
You can use the MsgBox function by itself (to simply display a message) or assign its result to a
variable. When you use the MsgBox function to return a result, the value represents the button
clicked by the user. The following example displays a message and an OK button, but doesn’t
return a result:
Sub MsgBoxDemo()
MsgBox “Macro finished with no errors.”
End Sub
To get a response from a message box, you can assign the results of the MsgBox function to a
variable. In the following code, I use some built-in constants (described in Table 12-3) to make it
easier to work with the values returned by MsgBox :
Sub GetAnswer()
Dim Ans As Integer
Ans = MsgBox(“Continue?”, vbYesNo)
Select Case Ans
Case vbYes
‘ ...[code if Ans is Yes]...
 
Search JabSto ::




Custom Search