Msg Box

The MsgBox dialog box allows you to display a message, such as "Please wait..." or "Access denied." The box can display one of four message icons, and there are many possibilities in the number and function of buttons that can be displayed.

The syntax of the MsgBox function is

MsgBox (prompt_text, buttons, title_text, helpfile, context)

where prompt_text is the message displayed within the box, buttons specifies the buttons to be displayed, and title_text is the title to be displayed in the Title Bar of the box. For information about helpfile and context, refer to Microsoft Excel Visual Basic Reference. The value of buttons determines the type of message icon and the number and type of response buttons; it also determines which button is the default button. The possible values are listed in Table 2-8. The values 0-5 specify the number and type of buttons, values 16-64 specify the type of message icon and values 0, 256, 512 specify which button is the default button. You add together one number from each group to form a value for buttons. For example, to specify a dialog box with a Warning Query icon, with Yes, No and Cancel buttons, and with the No button as default, the values 32 + 3 + 256 = 291.

Table 2-8. Values for the buttons Parameter of MsgBox

buttons Equivalent

Value

Constant

Description

0

vbOKOnly

Display OK button only.

1

vbOKCancel

Display OK and Cancel buttons.

2

vbAbortRetrylgnore

Display Abort, Retry and Ignore buttons.

3

vbYesNoCancel

Display Yes, No and Cancel buttons.

4

vbYesNo

Display Yes and No buttons.

5

vbRetryCancel

Display Retry and Cancel buttons.

0

No icon.

16

vbCritical

Display Critical Message icon.

32

vbQuestion

Display Warning Query icon.

48

vbExclamation

Display Warning Message icon.

64

vblnformation

Display Information Message icon.

0

vbDefaultButtonl

First button is default.

256

vbDefaultButton2

Second button is default.

512

vbDefaultButton3

Third button is default.

For example, the VBA expression,

MsgBox "You entered " & incr & "." & Chr(13) & Chr(13) & _ "That value is too large." & Chr(13) & Chr(13) & "Please try again.", 48

where the VBA variable incr has the value 50, produces the message box shown in Figure 2-14.

You entered 50,

You entered 50,

That valuéis too large. Please try again.

Figure 2-14. A Msgbox display.

The values of buttons are built-in constants—for example, the value 64 for buttons can be replaced by the variable name vblnformation. The same result, a dialog box with a Warning Query icon, with Yes, No and Cancel buttons and with the No button as default, can be obtained by using the expression vblnformation + vbYesNoCancel + vbDefaultButton2

in the MsgBox function instead of the value 323.

Was this article helpful?

0 0

Post a comment