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. 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

Constants Used in the MsgBox Function

Constant

Value

What It Does

vbOKOnly

0

Displays OK button only

vbOKCancel

1

Displays OK and Cancel buttons

vbAbortRetryIgnore

2

Displays Abort, Retry, and Ignore buttons

vbYesNoCancel

3

Displays Yes, No, and Cancel buttons

vbYesNo

4

Displays Yes and No buttons

vbRetryCancel

5

Displays Retry and Cancel buttons

vbCritical

16

Displays Critical Message icon

vbQuestion

32

Displays Warning Query icon

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

vbDefaultButton4

768

Fourth button is default

vbSystemModal

4096

System modal; all applications are suspended until the user responds to the message box

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

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 like to make things more difficult for yourself, use a value of 52 (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-2 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.

Figure 15-2:

The MsgBox function's buttons argument determines what appears in the message box.

Figure 15-2:

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. I also use the title argument to display a different title in the message box. Figure 15-3 shows the message box Excel displays when you execute this procedure.

Figure 15-3:

The dialog box displayed by the MsgBox function.

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.

Table 15-3

Constants Used as Return Values

for the MsgBox Function

Constant

Value What It Means

vbOK

1 User clicked OK.

vbCancel

2 User clicked Cancel.

vbAbort

3 User clicked Abort.

Figure 15-3:

The dialog box displayed by the MsgBox function.

Constant

Value

What It Means

vbRetry

4

User clicked Retry.

vbIgnore

5

User clicked Ignore.

vbYes

6

User clicked Yes.

vbNo

7

User clicked No.

Was this article helpful?

0 0

Post a comment