Summary of Usage of MsgBox in VBA

Source: Internet
Author: User

2. Use
Displays the information in a message box and waits for the user to click the button, returning the clicked button value (such as "OK" or "Cancel"). Typically used as a way to display variable values.
2. Grammar
MsgBox (Prompt[,buttons][,title][,helpfile,context])
Parameter description:
(1) Prompt, a required parameter, is a string that is displayed as the message text in a message box. The maximum length is approximately 1024 characters, which is determined by the width of the characters used. If the content of the prompt is more than one line, it is possible to separate lines by a carriage return (CHR (13)), a newline (Chr (10)), or a combination of carriage return and newline (Chr (10)) between each line.
(2) Buttons, optional parameters, which are the sum of the values of the numeric expression, specify the number and form of buttons to display, the icon style used, the default button, and the mandatory response of the message box, etc., which can be customized with this message box. If this argument is omitted, its default value is 0. The setting values are shown in the following table.
(3) Title, an optional parameter that represents the text displayed in the title bar of the message box. If this argument is omitted, the application name is placed in the title bar.
(4) Helpfile, optional parameter, is a string expression that provides a Help file. If there is a helpfile, then there must be a context.
(5) The Context, optional parameter, is a numeric expression that provides a Help topic. If there is a context, then there must be helpfile.
3. Button parameter
Constants for the button parameter in the MsgBox function
Constant Value Description
vbOKOnly 0 Displays the OK button only
vbOKCancel 1 Displays the OK and Cancel buttons
Vbabortretryignore 2 Displays the "Terminate", "Retry", and "Ignore" buttons
vbYesNoCancel 3 Displays Yes, no, and Cancel buttons
vbYesNo 4 Shows "yes" and "no" buttons
Vbretrycancel 5 display "Retry" and "Cancel" button
vbcritical 16 display "Key Info" icon
Vbquestion 32 Display "Warning ask" icon
Vbexclamation 48 Display "warning message" icon
vbinformation 64 display "notification message" icon
VbDefaultButton1 0 The first button is the default value (default setting)
VbDefaultButton2 256 The second button is a default value
VbDefaultButton3 512 The third button is the default value
VbDefaultButton4 768 The fourth button is a default value
vbApplicationModal 0 application is forced to return; The application is suspended until the user has a message box
Respond before continuing work
Vbsystemmodal 4096 system is forced to return; all applications are suspended until the user has a message box
The response to continue to work
vbMsgBoxHelpButton 16384 Adding the Help button to a message box
vbMsgBoxSetForeground 65536 Specifying a message box window as the foreground window
vbMsgBoxRight 524288 text is right-justified
vbMsgBoxRtlReading 1048576 Specifies that text should be displayed right-to-left in Hebrew and Arabic systems
Description
(1) The first set of values (0–5) describes the type and number of buttons displayed in the message box, the second set of values (16,32,48,64) describes the style of the icon, and the third set of values (0,256,512,768) indicates which button is the default, and the fourth set of values (0,4096) Determines the mandatory return of the message box. When these numbers are added to generate the buttons parameter values, only one number can be taken from each set of values.
(2) These constants are specified by VBA and can be used in the program code in place of the actual values.
4. Return value
Constants for the return value of the MsgBox function
Constant Value Description
vbOK 1 OK
Vbcancel 2 Cancel
Vbabort 3 Termination
Vbretry 4 Retry
Vbignore 5 ignored
Vbyes 6 is
Vbno 7 No
5. Related instructions
(1) If both the HelpFile and the context parameters are available, you can press the F1 key to view the appropriate Help topic for the context, and Excel will usually automatically add a Help button in the input box.
(2) If the Cancel button is displayed in a message box, pressing the ESC key is the same as clicking the Cancel button. If there is a Help button in the message box, provide the relevant help information.
(3) If you want to enter multiple parameters and omit some of the intermediate parameters, you must add a comma delimiter at the appropriate location.
6. Example
(1) Use the MsgBox function to display a critical error message in a dialog box with Yes and no buttons. The default button in the example is no, and the return value of the MsgBox function depends on which button the user presses. And assume that Demo.hlp is a Help file, which has a Help topic code of 1000.
Dim msg,style,title,help,ctxt,response,mystring
Msg= "Does want to continue?" Defining message Text
Style = vbYesNo + vbcritical + vbDefaultButton2 ' definition button
title = "MsgBox Demonstration" ' Defines the caption text
Help = "DEMO. HLP "' Definition Help file
Ctxt = 1000 ' Define Help topics
Response = MsgBox (MSG, Style, Title, help, Ctxt)
If Response = vbyes Then ' user presses ' yes '
MyString = "Yes" to complete an operation
Else ' user pressed ' no '
MyString = "No" completes an operation
End If
(2) Show only a message
MsgBox "Hello!"
(3) Assigning the result of a message box to a variable
Ans=msgbox ("Continue?", vbYesNo)
If MsgBox ("Continue?", vbYesNo) <>vbyes then Exit Sub
(4) Use a combination of constants, assign a group config variable, and set the second button as the default button
Config=vbyesno+vbquestion+vbdefaultbutton2
(5) To force a line break in a message, use the vbCrLf (or vbNewLine) constant in the text, separated by A & space and a character. Such as
MsgBox "The first line." & vbNewLine & "Second line."
(6) You can insert a tab character in a message box using the Vbtab constant. The following procedure uses a message box to display all values in a 5x5 range of cells, separates columns with Vbtab constants, and inserts a new row using the vbCrLf constant. Note that the MsgBox function displays up to 1024 characters, thus limiting the number of cells that can be displayed.
Option Explicit
Sub Showrangevalue ()
Dim MSG as String
Dim R As Integer, C as Integer
MSG = ""
for r = 1 to 5
For C = 1 to 5
msg = msg & Cells (R, c) & VbTab
Next C
msg = msg & vbCrLf
Next R
MsgBox MSG
End Sub
(7) Use a worksheet function in a message box statement and set the display's number format, as shown in the following statement:
MsgBox "Selection have" & M & "cells." & Chr & "The sum is:" & Application.worksheetfunction. Sum (Selection) & Chr & "The average is:" & Format (Application.WorksheetFunction.Average (Selection), "#, # #0. "), vbinformation," Selection Count & sum & Average "& Chr (13)

Summary of Usage of MsgBox in VBA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.