Msgbox usage in VB

Source: Internet
Author: User

Functions in VB can be implemented in the pop-up window.
Purpose: display the message in the dialog box, wait for the user to click the button, and return an integer to tell the user which button to click.
Syntax:
Msgbox (prompt [, Buttons] [, title] [, helpfile, context])
Parameter description:
The syntax of the msgbox function has the following naming parameters:
Prompt ------- required. String expression, used as a message displayed in the dialog box. The maximum length of a prompt is about 1024 characters, which is determined by the width of the characters used. If the content of prompt exceeds one line, you can use the carriage return (CHR (13), line feed (CHR (10), or the combination of carriage return and Line Break (CHR (13) between each line) & CHR (10) separates all rows.
Buttons ------- optional. A numeric expression is the sum of values. It specifies the number and form of buttons displayed, the icon style used, the default button, and the forced response of message boxes. If this parameter is omitted, the default value of buttons is 0.
Title ------- optional. The string expression displayed in the title bar of the dialog box. If the title is omitted, the application name is placed in the title bar.
Helpfile -------- optional. String expression to identify the help files used to provide context-related help to the dialog box. If helpfile is provided, context must also be provided.
Context ------- optional. Numeric expression, which is the help context number specified by the author of the Help file for the appropriate help topic. If context is provided, helpfile must also be provided.
Constant used for the button parameter in the msgbox Function
Constant Value description
Vbokonly 0 only displays the "OK" button
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 displays the "yes" and "no" buttons
Vbretrycancel 5 displays "retry" and "cancel" buttons
Vbcritical 16 displays the "Key Information" icon
Vbquestion 32 displays the "Warning inquiry" icon
Vbexclamation 48 displays the "warning message" icon
Vbinformation 64 displays the "Notification Message" icon
Vbdefaultbutton1 0 the first button is the default value (default)
Vbdefaultbutton2 256 the second button is the default value
Vbdefaultbutton3 512 the third button is the default value
Vbdefaultbutton4 768 the fourth button is the default value.
Vbapplicationmodal 0: The application is forcibly returned; the application is suspended until the user has
Respond to continue work
Vbsystemmodal 4096 is forcibly returned. All applications are suspended until the user calls
Respond to continue work
Vbmsgboxhelpbutton 16384 Add the Help button to the message box
Vbmsgboxsetforeground 65536 specifies the message box window as the foreground window
Vbmsgboxright 524288 right-aligned text
Vbmsgboxrtlreading 1048576 specifies that the text should be displayed from right to left in the Hebrew and Arabic Systems
Note:
(1) The first group value (0-5) describes the type and number of buttons displayed in the message box; the second group value (16, 32, 48, 64) describes the icon style; the third value (0,256,512,768) indicates which button is the default value, while the fourth value () determines the forced return of the message box. When these numbers are used to generate the buttons parameter value, only one number can be used for each group of values.
(2) These constants are specified by Visual Basic for Applications (VBA. As a result, you can use these constant names everywhere in the program code without using actual values. The actual value is equivalent to the constant name.
Return Value
Constant used for msgbox function return value
Constant Value description
Vbok 1 OK
Vbcancel 2 canceled
Vbabort 3 terminated
Vbretry 4 retry
Vbignore 5 ignore
Vbyes 6 is
Vbno 7 No
(1) If the helpfile and context parameters are provided at the same time, you can press the F1 key to view the Help topics corresponding to the context. In Excel, a help button is automatically added to the input box.
(2) If the "cancel" button is displayed in the message box, Press ESC and click "cancel. 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 parameters in the middle, you must add a comma (,) separator to the corresponding position.
Example
(1) (1) Use the msgbox function to display a critical error message in the dialog box with the "yes" and "no" buttons. The default button in the example is "no". The return value of the msgbox function depends on the button you press. Assume that demo. HLP is a help file with the help topic code 1000.
Dim MSG, style, title, help, ctxt, response, mystring
MSG = "do you want to continue? "'Defines the Message Text
Style = vbyesno + vbcritical + vbdefaultbutton2 'definition button
Title = "msgbox demonstration" 'defines the title text
Help = "demo. HLP" 'defines the Help File
Ctxt = 1000 'defines the help topic
Response = msgbox (MSG, style, title, help, ctxt)
If response = vbyes then 'user presses "yes"
Mystring = "yes" 'to complete an operation
Else 'user presses "no"
Mystring = "no" 'to complete an operation
End if
(2) only display a message
Msgbox "Hello !"
(3) Assign the result returned by the message box to the variable
Ans = msgbox ("Continue ?", Vbyesno)
If msgbox ("continue ?", Vbyesno) <> vbyes then exit sub
(4) use a combination of constants, assign values to the config variable of the group, and set the second button as the default button.
Config = vbyesno + vbquestion + vbdefaultbutton2
(5) to force line feed in a message, you can use the vbcrlf (or vbnewline) constant in the text and separate it with spaces. For example
Msgbox "This is the first line." & vbnewline & "second line ."
(6) You can use the vbtab constant in the message box to insert a tab. In the following process, a message box is used to display all values in the 5 × 5 cell area. The vbtab constant is used to separate columns and the vbcrlf constant is used to Insert a new row. Note that the msgbox function can display a maximum of 1024 characters, thus limiting the number of cells that can be displayed.
Option explicit
Sub showrangue ()
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 the worksheet function in the message box statement and set the displayed count format, as shown in the following statement:
Msgbox "selection has" & M & "cells. "& CHR (13) &" the sum is: "& application. worksheetfunction. sum (selection) & CHR (13) & "the average is:" & format (application. worksheetfunction. average (selection), "#,## 0.00"), vbinformation, "selection count & sum & average" & CHR (13)

Example
Private sub form_load () 'event form Loading

Msgbox ("pop-up window") 'sets the pop-up window

End sub'

Code in the image: msgbox "Warning content! ", 26," warning! "
Functions in VB can be implemented in the pop-up window.
Purpose: display the message in the dialog box, wait for the user to click the button, and return an integer to tell the user which button to click.
Syntax:
Msgbox (prompt [, Buttons] [, title] [, helpfile, context])
Parameter description:
The syntax of the msgbox function has the following naming parameters:
Prompt ------- required. String expression, used as a message displayed in the dialog box. The maximum length of a prompt is about 1024 characters, which is determined by the width of the characters used. If the content of prompt exceeds one line, you can use the carriage return (CHR (13), line feed (CHR (10), or the combination of carriage return and Line Break (CHR (13) between each line) & CHR (10) separates all rows.
Buttons ------- optional. A numeric expression is the sum of values. It specifies the number and form of buttons displayed, the icon style used, the default button, and the forced response of message boxes. If this parameter is omitted, the default value of buttons is 0.
Title ------- optional. The string expression displayed in the title bar of the dialog box. If the title is omitted, the application name is placed in the title bar.
Helpfile -------- optional. String expression to identify the help files used to provide context-related help to the dialog box. If helpfile is provided, context must also be provided.
Context ------- optional. Numeric expression, which is the help context number specified by the author of the Help file for the appropriate help topic. If context is provided, helpfile must also be provided.
Constant used for the button parameter in the msgbox Function
Constant Value description
Vbokonly 0 only displays the "OK" button
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 displays the "yes" and "no" buttons
Vbretrycancel 5 displays "retry" and "cancel" buttons
Vbcritical 16 displays the "Key Information" icon
Vbquestion 32 displays the "Warning inquiry" icon
Vbexclamation 48 displays the "warning message" icon
Vbinformation 64 displays the "Notification Message" icon
Vbdefaultbutton1 0 the first button is the default value (default)
Vbdefaultbutton2 256 the second button is the default value
Vbdefaultbutton3 512 the third button is the default value
Vbdefaultbutton4 768 the fourth button is the default value.
Vbapplicationmodal 0: The application is forcibly returned; the application is suspended until the user has
Respond to continue work
Vbsystemmodal 4096 is forcibly returned. All applications are suspended until the user calls
Respond to continue work
Vbmsgboxhelpbutton 16384 Add the Help button to the message box
Vbmsgboxsetforeground 65536 specifies the message box window as the foreground window
Vbmsgboxright 524288 right-aligned text
Vbmsgboxrtlreading 1048576 specifies that the text should be displayed from right to left in the Hebrew and Arabic Systems
Note:
(1) The first group value (0-5) describes the type and number of buttons displayed in the message box; the second group value (16, 32, 48, 64) describes the icon style; the third value (0,256,512,768) indicates which button is the default value, while the fourth value () determines the forced return of the message box. When these numbers are used to generate the buttons parameter value, only one number can be used for each group of values.
(2) These constants are specified by Visual Basic for Applications (VBA. As a result, you can use these constant names everywhere in the program code without using actual values. The actual value is equivalent to the constant name.
Return Value
Constant used for msgbox function return value
Constant Value description
Vbok 1 OK
Vbcancel 2 canceled
Vbabort 3 terminated
Vbretry 4 retry
Vbignore 5 ignore
Vbyes 6 is
Vbno 7 No
(1) If the helpfile and context parameters are provided at the same time, you can press the F1 key to view the Help topics corresponding to the context. In Excel, a help button is automatically added to the input box.
(2) If the "cancel" button is displayed in the message box, Press ESC and click "cancel. 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 parameters in the middle, you must add a comma (,) separator to the corresponding position.
Example
(1) (1) Use the msgbox function to display a critical error message in the dialog box with the "yes" and "no" buttons. The default button in the example is "no". The return value of the msgbox function depends on the button you press. Assume that demo. HLP is a help file with the help topic code 1000.
Dim MSG, style, title, help, ctxt, response, mystring
MSG = "do you want to continue? "'Defines the Message Text
Style = vbyesno + vbcritical + vbdefaultbutton2 'definition button
Title = "msgbox demonstration" 'defines the title text
Help = "demo. HLP" 'defines the Help File
Ctxt = 1000 'defines the help topic
Response = msgbox (MSG, style, title, help, ctxt)
If response = vbyes then 'user presses "yes"
Mystring = "yes" 'to complete an operation
Else 'user presses "no"
Mystring = "no" 'to complete an operation
End if
(2) only display a message
Msgbox "Hello !"
(3) Assign the result returned by the message box to the variable
Ans = msgbox ("Continue ?", Vbyesno)
If msgbox ("continue ?", Vbyesno) <> vbyes then exit sub
(4) use a combination of constants, assign values to the config variable of the group, and set the second button as the default button.
Config = vbyesno + vbquestion + vbdefaultbutton2
(5) to force line feed in a message, you can use the vbcrlf (or vbnewline) constant in the text and separate it with spaces. For example
Msgbox "This is the first line." & vbnewline & "second line ."
(6) You can use the vbtab constant in the message box to insert a tab. In the following process, a message box is used to display all values in the 5 × 5 cell area. The vbtab constant is used to separate columns and the vbcrlf constant is used to Insert a new row. Note that the msgbox function can display a maximum of 1024 characters, thus limiting the number of cells that can be displayed.
Option explicit
Sub showrangue ()
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 the worksheet function in the message box statement and set the displayed count format, as shown in the following statement:
Msgbox "selection has" & M & "cells. "& CHR (13) &" the sum is: "& application. worksheetfunction. sum (selection) & CHR (13) & "the average is:" & format (application. worksheetfunction. average (selection), "#,## 0.00"), vbinformation, "selection count & sum & average" & CHR (13)

Example
Private sub form_load () 'event form Loading

Msgbox ("pop-up window") 'sets the pop-up window

End sub'

Code in the image: msgbox "Warning content! ", 26," warning! "

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.