Excel 2019 Bible

(singke) #1

Chapter 44: Creating UserForms


44


Constant Value Description

vbQuestion (^32) Displays Query icon (a question mark)
VBExclamation (^48) Displays Warning Message icon
vbInformation (^64) Displays Information Message icon
vbDefaultButton1 (^0) First button is default
vbDefaultButton2 (^256) Second button is default
vbDefaultButton3 (^512) Third button is default
The following example uses a combination of constants to display a message box with a Yes
button and a No button (vbYesNo) and a question mark icon (vbQuestion). The second
button (the No button) is designated as the default button (vbDefaultButton2), which
is the one that is executed if the user presses Enter. For simplicity, these constants are
assigned to the Config variable, and Config is then used as the second argument in the
MsgBox function.
Sub GetAnswer()
Config = vbYesNo + vbQuestion + vbDefaultButton2
Ans = MsgBox("Process the monthly report?", Config)
If Ans = vbYes Then RunReport
If Ans = vbNo Then Exit Sub
End Sub
Figure 44.4 shows how this message box appears when the GetAnswer procedure is
executed. If the user clicks the Yes button, the routine executes the procedure named
RunReport (which is not shown). If the user clicks the No button (or presses Enter), the
procedure is ended with no action. Because the title argument was omitted in the MsgBox
function, Excel uses the default title (Microsoft Excel).
FIGURE 44.4
The second argument of the MsgBox function determines what appears in the message box.
The procedure that follows is another example of using the MsgBox function:
Sub GetAnswer2()
Msg = "Do you want to process the monthly report?"

Free download pdf