Parameters and usage of MsgBox in VBA

Source: Internet
Author: User

1 , Role

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 Parameters
Constants for the button parameter in the MsgBox function

Symbolic constants Value Role
Button type vbOKOnly 0 Show the OK button only
vbOKCancel 1 Show "OK" and "Cancel" button
Vbabortretryignore 2 Show "Terminate", "Retry", and "Ignore" buttons
vbYesNoCancel 3 Show Yes, no, and Cancel buttons
vbYesNo 4 Show Yes, no button
Vbretrycancel 5 Show "Retry" and "Cancel" button
 
Icon Style vbcritical 16 Display the critical message icon (System fork icon)
Vbquestion 32 Display the warning query icon (System question mark icon)
Vbexclamation 48 Display the warning message icon (System exclamation icon)
vbinformation 64 Display the information message icon (System information icon)
       
Default button Vbdefauitbutton1 0 The first button is a default value
Vbdefauitbutton2 256 The second button is a default value
Vbdefauitbutton3 512 The third button is a default value
Vbdefauitbutton4 768 The fourth button is a default value
       
Forced return Vbapplicationmode1 0 Application forced return
Vbsystemmode1 4096 System forced return
       
Other types vbMsgBoxHelpButton 16384 Add a Help button to the dialog box
vbMsgBoxSetForeground 65535 Specify dialog window as foreground window
vbMsgBoxRight 524288 The text of the dialog box is right-justified
Vbmsgboxrtireading 1048576 Specifies that the elements in the dialog box are displayed from right to left


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

Are you sure

Vbcancel

2

Cancel

Vbabort

3

Terminate

Vbretry

4

Retry

Vbignore

5

Ignore

Vbyes

6

Is

Vbno

7

Whether

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.

  1. Dim msg,style,title,help,ctxt,response,mystring
  2. ' Define message text
  3. Msg= "Does want to continue?"
  4. ' Define button
  5. Style = vbYesNo + vbcritical + vbDefaultButton2
  6. ' Define title text
  7. Title = "MsgBox demonstration"
  8. ' Define Help file
  9. Help = "DEMO. HLP "
  10. ' Define Help topics
  11. Ctxt = 1000
  12. Response = MsgBox (MSG, Style, Title, help, Ctxt)
  13. If Response = vbyes Then ' user presses ' yes '
  14. MyString = "Yes"
  15. Else ' user pressed ' no '
  16. MyString = "No"
  17. End If

(2) Show only a message

    1. MsgBox "Hello!"


(3) Assigning the result of a message box to a variable

    1. Ans=msgbox ("Continue?", vbYesNo)
    2. 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

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

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

    1. OPTION&NBSP;EXPLICIT&NBSP;&NBSP;
    2. Sub showrangevalue ()   
    3. &NBSP;&NBSP;DIM&NBSP;MSG&NBSP;AS&NBSP;STRING&NBSP;&NBSP;
    4.    dim r as integer, c as integer  
    5. &NBSP;&NBSP;MSG  =  "" &NBSP;&NBSP;
    6.   for r = 1 to 5  
    7. &NBSP;&NBSP;&NBSP;&NBSP;FOR&NBSP;C&NBSP;=&NBSP;1&NBSP;TO&NBSP;5&NBSP;&NBSP;
    8.        msg = msg & cells (r, c)  & vbTab  
    9. &NBSP;&NBSP;&NBSP;&NBSP;NEXT&NBSP;C&NBSP;&NBSP;
    10.     Msg  = msg & vbcrlf  
    11. &NBSP;&NBSP;NEXT&NBSP;R&NBSP;&NBSP;
    12.   msgbox msg  
    13. END&NBSP;SUB&NBSP;&NBSP;

(7) Use a worksheet function in a message box statement and set the display's number format, as shown in the following statement:

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

Source: http://blog.csdn.net/bright_chen7/article/details/6248715

Reference: https://www.baidu.com/link?url=lRwVSdVDBPcm5ww-CkxqlyM4ColOU1VodWiHGsA-LgQnmYahXESn7nydeZmAo9rz&wd= &eqid=c33f9e7100001a8b00000004584f93a7

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