Page 198 - Excel Data Analysis
P. 198
10 537547 Ch09.qxd 3/4/03 12:13 PM Page 184
EXCEL DATA ANALYSIS
DISPLAY A MESSAGE BOX
ou can use the MsgBox function to display pop-up in quotes or use a variable. You can combine values by
message boxes when a VBA procedure executes. The using the concatenation operator (&), as in the example
Y MsgBox function does two things: It displays a dialog MsgBox("Total Sum: " & TotalSum).
box to provide information to the user and returns a value The optional Buttons argument enables you to specify a
that indicates the response from the user. You capture the constant value, which displays the buttons and icons on the
user response by assigning the results of the MsgBox message box. If you do not specify a button constant, the
function to a variable. For example, the code MsgBox function uses the default vbOKOnly, which
UserResponse = MsgBox("Do you want to displays only the OK button.
continue?") assigns a value between 1 and 7 indicating
the user response to the UserResponse variable. If the The optional Title argument contains the text that
user selects the OK button, the MsgBox function returns a displays on the title bar of the message box. If you omit this
constant value of vbOK or 1. argument, Excel displays a default value of Microsoft Excel.
The MsgBox function has five different arguments: The final two optional arguments let you add help functions
Prompt, Buttons, Title, Helpfile, and Context. to the message box. Helpfile specifies the name of the
All but the first argument are optional. The Prompt help file, and Context specifies the context ID of the help
argument indicates the value that displays in the message topic to display.
box. You can make this argument a text string and enclose it
DISPLAY A MESSAGE BOX
⁄ Create a new subroutine. ‹ Declare other variables › Type MsgVar = ■ You can type + to separate
needed for the subroutine. MsgBox("Text Prompt", multiple button constant
¤ Type Dim MsgVar As buttons, "Text Title"), values.
Integer, replacing MsgVar Note: See "Create a Macro Using replacing "Text Prompt"
with the variable to receive the Visual Basic Editor" for more on with the prompt, buttons
the MsgBox return value. subroutines and "Declare a Variable" with the button constant, and
for more on variables.
"Text Title" with the
title.
184