Customizing message boxes. Microsoft Excel VBA Programming
Annons
Annons
238
Part IV: Communicating with Your Users
Figure 15-2 shows how it looks. When you execute this procedure, the Ans variable is assigned a value of either vbYes or vbNo, depending on which button the user clicks. The Select Case statement uses the Ans value to determine which action the code should perform.
Figure 15-2:
A simple message box, with two buttons.
You can also use the MsgBox function result without using a variable, as the following example demonstrates:
Sub GetAnswer2()
If MsgBox(“Continue?”, vbYesNo) = vbYes Then
‘ ...[code if Yes is clicked]...
Else
‘ ...[code if Yes is not clicked]...
End If
End Sub
Customizing message boxes
The flexibility of the buttons argument makes it easy to customize your message boxes. You can specify which buttons to display, determine whether an icon appears, and decide which button is the default (the default button is
“clicked” if the user presses Enter).
Table 15-2 lists some of the built-in constants you can use for the buttons argument. If you prefer, you can use the value rather than a constant (but I think using the built-in constants is a lot easier).
Table 15-2
Constant
vbOKOnly vbOKCancel vbAbortRetryIgnore vbYesNoCancel vbYesNo
Constants Used in the MsgBox Function
1
2
3
Value
0
4
What It Does
Displays OK button only.
Displays OK and Cancel buttons.
Displays Abort, Retry, and Ignore buttons.
Displays Yes, No, and Cancel buttons.
Displays Yes and No buttons.
Constant
vbRetryCancel vbCritical vbQuestion vbExclamation vbInformation vbDefaultButton1 vbDefaultButton2 vbDefaultButton3 vbDefaultButton4
64
0
256
512
768
Value
5
16
32
48
Chapter 15: Simple Dialog Boxes
239
What It Does
Displays Retry and Cancel buttons.
Displays Critical Message icon.
Displays Warning Query icon.
Displays Warning Message icon.
Displays Information Message icon.
First button is default.
Second button is default.
Third button is default.
Fourth button is default.
To use more than one of these constants as an argument, just connect them with a + operator. For example, to display a message box with Yes and No buttons and an exclamation icon, use the following expression as the second
MsgBox argument: vbYesNo + vbExclamation
Or, if you prefer to make your code less understandable, use a value of 52
(that is, 4 + 48).
The following example uses a combination of constants to display a message box with a Yes button and a No button (vbYesNo) as well as a question mark icon (vbQuestion). The constant vbDefaultButton2 designates the second button (No) as the default button — that is, the button that is clicked if the user presses Enter. For simplicity, I assign these constants to the Config variable and then use Config as the second argument in the MsgBox function:
Sub GetAnswer3()
Dim Config As Integer
Dim Ans As Integer
Config = vbYesNo + vbQuestion + vbDefaultButton2
Ans = MsgBox(“Process the monthly report?”, Config)
If Ans = vbYes Then RunReport
End Sub
Figure 15-3 shows the message box Excel displays when you execute the
GetAnswer3 procedure. 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 routine ends with no action. Because I omitted the title argument in the MsgBox function, Excel uses the default title,
Microsoft Excel.
240
Part IV: Communicating with Your Users
Figure 15-3:
The MsgBox function’s buttons argument determines what appears in the message box.
The following routine provides another example of using the MsgBox function:
Sub GetAnswer4()
Dim Msg As String, Title As String
Dim Config As Integer, Ans As Integer
Msg = “Do you want to process the monthly report?”
Msg = Msg & vbNewLine & vbNewLine
Msg = Msg & “Processing the monthly report will “
Msg = Msg & “take approximately 15 minutes. It “
Msg = Msg & “will generate a 30-page report for “
Msg = Msg & “all sales offices for the current “
Msg = Msg & “month.”
Title = “XYZ Marketing Company”
Config = vbYesNo + vbQuestion
Ans = MsgBox(Msg, Config, Title)
If Ans = vbYes Then RunReport
End Sub
This example demonstrates an efficient way to specify a longer message in a message box. I use a variable (Msg) and the concatenation operator (&) to build the message in a series of statements. The vbNewLine constant inserts a line break character that starts a new line (use it twice to insert a blank line). I also use the title argument to display a different title in the message box. Figure 15-4 shows the message box Excel displays when you execute this procedure.
Previous examples have used constants (such as vbYes and vbNo) for the return value of a MsgBox function. Besides these two constants, Table 15-3 lists a few others.
And that’s pretty much all you need to know about the MsgBox function.
Use message boxes with caution, though. There’s usually no reason to display message boxes that serve no purpose. For example, people tend to get annoyed when they see a message box every day that reads, “Good morning, thanks for loading the Budget Projection workbook.”
Ladda ner
Annons