Unformatted text preview:

METBD 050 VBA – Message Boxes E. R. Evans, Jr. – 11/8/01 Page 1 of 3 F:\METBD050-CommonFiles\HO-050-VBA-MsgBox.doc The MsgBox function provides a way to display messages to the user and have them respond. The message box displays a message to the user and waits for a response. The message box then returns a value to the program. This value depends on which button is picked by the user. A sample message box is shown in Figure 1. In this case, the message box has two buttons, a text message, an icon, and a title. The syntax for the MsgBox function is shown below. The values within the brackets, [ ], are optional. MsgBox message [, [buttons][, title][, helpfile, context ]] The statement used to create the dialog box in Figure 1 is : MsgBox “The value is too high.”, 17, “Error” The buttons option is a number that tells VB what buttons and icon to display as well as which button is the default button. The value is the sum of three numbers: the button component + the icon component + the default button component. Only one number can be used for each component. See the table on the next page for the component values. Example: Compute the value to display both an OK and Cancel button along with the Stop icon and having the Cancel button (second button) be the default button. The argument is computed as: 1 + 16 + 256 = 273. Alternately, you can add the constants together which would be: vbOKCancel + vbCritical + vbDefaultButton2. The second method is easier to remember, but longer to type. message The string expression displayed as the message in the dialog box. The maximum length is 1024 characters. buttons A numeric expression that is the sum of values that describe the display and behavior of the message box. title String expression that will appear in the dialog box title bar. If omitted, the project name appears. helpfile String naming the help file associated with this message. context Required if helpfile is used. Provides the context for context-sensitive help. Two buttons Title Icon Message Figure 1: Sample Message BoxMETBD 050 VBA – Message Boxes E. R. Evans, Jr. – 11/8/01 Page 2 of 3 F:\METBD050-CommonFiles\HO-050-VBA-MsgBox.doc The response from the button pick can be assigned to a variable. For instance, the following command starts the message box and saves the value of the button picked (1 for OK or 2 for Cancel) to the integer variable ‘response.’ response = MsgBox("The value is too high.", 17, "Error") This code can be used in conjunction with an If-block to react to the user’s response. Values for Creating Command Buttons Value Constant Command Buttons 0 vbOKOnly Creates only an OK button in the message box. 1 vbOKCancel Creates OK and Cancel buttons. 2 vbAbortRetryIgnore Creates Abort, Retry and Ignore buttons. 3 vbYesNoCancel Creates Yes, No and Cancel buttons. 4 vbYesNo Creates Yes and No buttons. 5 vbRetryCancel Creates Retry and Cancel buttons. 16384 vbMsgBoxHelpButton Creates a Help button. Values for Creating Icons Value Constant Icon 16 vbCritical Displays the Stop icon 32 vbQuestion Displays the Question icon 48 vbExclamation Displays the Exclamation icon 64 vbInformation Displays the Information icon Values for setting the Default Command Button Value Constant Default Setting 0 vbDefaultButton1 Sets the first button as default. 256 VbDefaultButton2 Sets the second button as default. 512 VbDefaultButton3 Sets the third button as default. 768 VbDefaultButton4 Sets the fourth button as default. Values Returned from a Message Box Button Selected Return Value Return Constant OK 1 vbOK Cancel 2 vbCancel Abort 3 vbAbort Retry 4 vbRetry Ignore 5 vbIgnore Yes 6 vbYes No 7 vbNoMETBD 050 VBA – Message Boxes E. R. Evans, Jr. – 11/8/01 Page 3 of 3 F:\METBD050-CommonFiles\HO-050-VBA-MsgBox.doc Example: Write a procedure that asks the user if they want to print the worksheet when they pick the menu option to close the workbook. Sub Workbook_BeforeClose () Response = MsgBox (“Do you want to print this sheet before you quit?”, 36, “Print Option”) ‘ If Response = 6 Then Selection.PrintOut Copies:=1, Collate:=True End If End Sub If the user picks the YES button (6) then the worksheet prints before the workbook closes. Otherwise, the workbook closes without


View Full Document

PSU METBD 050 - Message Boxes

Download Message Boxes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Message Boxes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Message Boxes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?