VBA/Excel/Access/Word/Language Basics/MsgBox
Содержание
- 1 Adding a Help Button to a Message Box: use the vbMsgBoxHelpButton constant
- 2 Add the Information icon
- 3 After you have placed the return value into a variable, you can easily introduce logic into your program to respond to the user"s selection
- 4 A single statement splits over four lines.
- 5 Buttons and Icons in the MsgBox Function
- 6 Call MsgBox function by using its parameter name
- 7 Check MsgBox result
- 8 determines which button was pressed.
- 9 Each of the available buttons will produce an integer result.
- 10 Getting a response from a message box with Select Case
- 11 Keep the message box visible while the user works with other open applications
- 12 MsgBox"s third parameter is the message box"s title. Its fourth and fifth parameters are the Help file and context ID
- 13 MsgBox "Welcome to VBA", vbYesNoCancel, "VBA Message Box"
- 14 MsgBox with parameter Name: title:=myTitle, prompt:=question, buttons:=myButtons, helpfile:= "HelpX.hlp",context:=55
- 15 MsgBox with three parameters
- 16 Ok To Overwrite
- 17 Pass single parameter to MsgBox
- 18 Returning Values from the MsgBox Function
- 19 Save the return value from MsgBox to a variable
- 20 Set default button for MsgBox
- 21 Specifying a Help File for a Message Box
- 22 Specifying a Title for a Message Box
- 23 The message box displays Yes, No, and Cancel buttons
- 24 The MsgBox buttons argument"s settings
- 25 Use Chr() function to format MsgBox
- 26 use the MsgBox function result without using a variable
- 27 Use & to link messages
- 28 Using the MsgBox Function: MsgBox (prompt [, buttons] [, title], [, helpfile, context])
- 29 Using the MsgBox Function with Arguments
- 30 Using the Select Case Statement to check the MsgBox button clicked
Adding a Help Button to a Message Box: use the vbMsgBoxHelpButton constant
<source lang="vb">
Sub help()
MsgBox("Do you want to delete this workbook?", vbYesNo _ + vbCritical + vbDefaultButton2 + vbMsgBoxHelpButton, _ "Delete Workbook")
End Sub
</source>
Add the Information icon
<source lang="vb">
Sub infoSub()
msgBox "Welcome to VBA", vbYesNoCancel + vbInformation, "VBA Message Box"
End Sub
</source>
After you have placed the return value into a variable, you can easily introduce logic into your program to respond to the user"s selection
<source lang="vb">
Sub MessageBoxAnswer()
Dim intAnswer As Integer intAnswer = msgBox("Are You Sure?", vbYesNoCancel + vbQuestion, _ "Please Respond") Select Case intAnswer Case vbYes msgBox "I"m Glad You are Sure!!" Case vbNo msgBox "Why Aren"t You Sure??" Case vbCancel msgBox "You Coward! You Bailed Out!!" End Select
End Sub
</source>
A single statement splits over four lines.
<source lang="vb">
Sub single1()
MsgBox "Can"t find " & UCase(SHORTCUTMENUFILE) & ThisWorkbook.Path & vbCrLf & vbCrLf & _ "You may need to reinstall BudgetMan", vbCritical, APPNAME
End Sub
</source>
Buttons and Icons in the MsgBox Function
<source lang="vb">
Constant Value Description vbOKOnly 0 Displays OK button only. vbOKCancel 1 Displays OK and Cancel buttons. vbAbortRetryIgnore 2 Displays Abort, Retry, and Ignore buttons. vbYesNoCancel 3 Displays Yes, No, and Cancel buttons. vbYesNo 4 Displays Yes and No buttons. vbRetryCancel 5 Displays Retry and Cancel buttons. vbCritical 16 Displays Critical Message icon. vbQuestion 32 Displays Warning Query icon. vbExclamation 48 Displays Warning Message icon. vbInformation 64 Displays Information Message icon. vbDefaultButton1 0 First button is default. vbDefaultButton2 256 Second button is default. vbDefaultButton3 512 Third button is default. vbDefaultButton4 768 Fourth button is default. vbApplicationModal 0 Application modal; the user must respond to the message box before continuing work in the current application. vbSystemModal 4096 System modal; all applications are suspended until the user responds to the message box. vbMsgBoxHelpButton 16384 Add Help button to the message box. VbMsgBoxSetForeground 65536 Specify the message box window as the foreground window. vbMsgBoxRight 524288 Text is right aligned. vbMsgBoxRtlReading 1048576 Specify that text should appear as right-to-left reading on Hebrew and Arabic systems.
</source>
Call MsgBox function by using its parameter name
<source lang="vb">
Sub MsgBoxParameterName()
Dim myTitle As String myTitle = "title" Dim questin As String question = "question" Dim myButtons As Integer myButtons = vbYesNo + vbQuestion + vbDefaultButton2 MsgBox Title:=myTitle, prompt:=question, Buttons:=myButtons
End Sub
</source>
Check MsgBox result
<source lang="vb">
"Button Selected Constant Value "OK vbOK 1 "Cancel vbCancel 2 "Abort vbAbort 3 "Retry vbRetry 4 "Ignore vbIgnore 5 "Yes vbYes 6 "No vbNo 7
Sub MsgBoxDemo()
Dim Answer As Long Answer = MsgBox("Message", vbQuestion + vbOKCancel, "Error") If Answer = vbOK Then MsgBox "OK" End If If Answer = vbCancel Then MsgBox "Cancel" End If
End Sub
</source>
determines which button was pressed.
<source lang="vb">
Sub messageBoxTest()
Dim intButtonPressed As Integer intButtonPressed = msgBox("Welcome to VBA", vbYesNo, "Message Box Test") If intButtonPressed = vbYes Then msgBox "Yes was selected" Else msgBox "No was selected" End If
End Sub
</source>
Each of the available buttons will produce an integer result.
<source lang="vb">
Constant Value Description vbOK 1 OK vbCancel 2 Cancel vbAbort 3 Abort vbRetry 4 Retry vbIgnore 5 Ignore vbYes 6 Yes vbNo 7 No
</source>
Getting a response from a message box with Select Case
<source lang="vb"> Sub GetAnswer() Dim Ans As Integer Ans = MsgBox("Continue?", vbYesNo) Select Case Ans Case vbYes " ...[code if Ans is Yes]... Case vbNo " ...[code if Ans is No]... End Select End Sub </source>
Keep the message box visible while the user works with other open applications
<source lang="vb">
Sub modelMsg()
MsgBox "How are you?", vbOKOnly + vbSystemModal, "System Modal"
End Sub
</source>
MsgBox"s third parameter is the message box"s title. Its fourth and fifth parameters are the Help file and context ID
<source lang="vb">
Sub m()
msgBox "This is a Message", vbInformation, "This is a Title"
End Sub
</source>
MsgBox "Welcome to VBA", vbYesNoCancel, "VBA Message Box"
<source lang="vb">
Sub yesNoSub()
msgBox "Welcome to VBA", vbYesNoCancel, "VBA Message Box"
End Sub
</source>
MsgBox with parameter Name: title:=myTitle, prompt:=question, buttons:=myButtons, helpfile:= "HelpX.hlp",context:=55
<source lang="vb">
Sub msgBoxPa()
Dim myTitle As String myTitle = "title" Dim questin As String question = "question" Dim myButtons As Integer myButtons = vbYesNo + vbQuestion + vbDefaultButton2 MsgBox Title:=myTitle, prompt:=question, Buttons:=myButtons, HelpFile:="HelpX.hlp", Context:=55
End Sub
</source>
MsgBox with three parameters
<source lang="vb">
Sub MsgYesNo2()
Dim question As String Dim myButtons As Integer Dim myTitle As String question = "Do you want to open a new report?" myButtons = vbYesNo + vbQuestion + vbDefaultButton2 myTitle = "New report" MsgBox question, myButtons, myTitle
End Sub
</source>
Ok To Overwrite
<source lang="vb">
Function OkToOverwrite(sFullName As String) As Boolean
Dim sMsg As String Dim nButtons As Long Dim nResponse As Long Dim bOverwrite As Boolean bOverwrite = False sMsg = sFullName & " already exists. Do you want to overwrite it?" nButtons = vbYesNoCancel + vbExclamation + vbDefaultButton2 nResponse = MsgBox(sMsg, nButtons, "Overwrite File?") If nResponse = vbYes Then bOverwrite = True End If OkToOverwrite = bOverwrite
End Function
</source>
Pass single parameter to MsgBox
<source lang="vb">
Sub Test1()
MsgBox "Please make sure that the printer is switched on"
End Sub
</source>
Returning Values from the MsgBox Function
<source lang="vb">
Sub MsgYesNo3()
Dim question As String Dim myButtons As Integer Dim myTitle As String Dim myChoice As Integer question = "Do you want to open a new report?" myButtons = vbYesNo + vbQuestion + vbDefaultButton2 myTitle = "New report" myChoice = MsgBox(question, myButtons, myTitle) MsgBox myChoice
End Sub
</source>
Save the return value from MsgBox to a variable
<source lang="vb">
Sub Informant2()
Dim myPrompt As String Dim town As String Const myTitle = "Enter data" myPrompt = "Place of birth:" & Chr(13) & "(e.g., Boston, Great Falls, etc.)" town = InputBox(myPrompt, myTitle) MsgBox "You were born in " & town & ".", , "Your response"
End Sub
</source>
Set default button for MsgBox
<source lang="vb">
Sub defaultSub()
MsgBox "Do you want to proceed?", vbYesNo + vbQuestion + vbDefaultButton2
End Sub
</source>
Specifying a Help File for a Message Box
<source lang="vb">
Sub helpFile()
Dim lngQ As Long lngQ = MsgBox("Do you want to delete this workbook?", vbYesNo _ + vbCritical + vbDefaultButton2 + vbMsgBoxHelpButton, _ "Delete Workbook", "c:\Help.chm", 1012)
End Sub
</source>
Specifying a Title for a Message Box
<source lang="vb">
Sub title()
MsgBox("Do you want to delete this workbook?", vbYesNo _ + vbCritical + vbDefaultButton2, "Delete Workbook 12.39")
End Sub
</source>
The message box displays Yes, No, and Cancel buttons
<source lang="vb">
Sub MessageBoxFunction()
Dim intAnswer As Integer intAnswer = msgBox("Are You Sure?", vbYesNoCancel + vbQuestion, _ "Please Respond")
End Sub
</source>
The MsgBox buttons argument"s settings
<source lang="vb">
Default button settings vbDefaultButton1 0 The first button is default vbDefaultButton2 256 The second button is default vbDefaultButton3 512 The third button is default vbDefaultButton4 768 The fourth button is default Message box modality vbApplicationModal 0 The user must respond to the message before continuing to work in the current application. vbSystemModal 4096 On Win16 systems, this constant is used to prevent the user from interacting with any other window until he or she dismisses the message box. On Win32 systems, this constant works like the vbApplicationModal constant (see above) with the following exception: The message box always remains on top of any other programs you may have running. Other MsgBox display settings vbMsgBoxHelpButton 16384 Adds the Help button to the message box vbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window vbMsgBoxRight 524288 Text is right aligned vbMsgBoxRtlReading 1048576 Text appears as right-to-left reading on Hebrew and Arabic systems Sub buttonMsg()
MsgBox "How are you?", vbOKOnly + vbApplicationModal, "Application Modal"
End Sub
</source>
Use Chr() function to format MsgBox
<source lang="vb">
Sub charMsgBox()
MsgBox "A." & Chr(13) & "B ""C"" D" & Chr(13) & "E." & Chr(13) & "F."
End Sub
</source>
use the MsgBox function result without using a variable
<source lang="vb"> Sub GetAnswer2() If MsgBox("Continue?", vbYesNo) = vbYes Then " ...[code if Yes is clicked]... Else " ...[code if Yes is not clicked]... End If End Sub </source>
Use & to link messages
<source lang="vb">
Sub MyMessage()
MsgBox "A." & Chr(13) _ & "B ""C"" E" & Chr(13) _ & "F." & Chr(13) _ & "G."
End Sub
</source>
Using the MsgBox Function: MsgBox (prompt [, buttons] [, title], [, helpfile, context])
<source lang="vb">
Sub msgSub()
MsgBox "The procedure is complete."
End Sub "Formatting the Message Box Sub msgBoxQuotation()
MsgBox "A ""B"" C."
End Sub
</source>
Using the MsgBox Function with Arguments
<source lang="vb">
Sub MsgYesNo()
Dim question As String Dim myButtons As Integer question = "Do you want to open a new report?" myButtons = vbYesNo + vbQuestion + vbDefaultButton2 MsgBox question, myButtons
End Sub
</source>
Using the Select Case Statement to check the MsgBox button clicked
<source lang="vb">
Sub TestButtons()
Dim question As String Dim bts As Integer Dim myTitle As String Dim myButton As Integer question = "Do you want to preview the report now?" bts = vbYesNoCancel + vbQuestion + vbDefaultButton1 myTitle = "Report" myButton = MsgBox(prompt:=question, Buttons:=bts, Title:=myTitle) Select Case myButton Case 6 MsgBox "You can review the report now." Case 7 MsgBox "You can review the report later." Case Else MsgBox "You pressed Cancel." End Select
End Sub
</source>