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
Sub help()
MsgBox("Do you want to delete this workbook?", vbYesNo _
+ vbCritical + vbDefaultButton2 + vbMsgBoxHelpButton, _
"Delete Workbook")
End Sub
Add the Information icon
Sub infoSub()
msgBox "Welcome to VBA", vbYesNoCancel + vbInformation, "VBA Message Box"
End Sub
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
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
A single statement splits over four lines.
Sub single1()
MsgBox "Can"t find " & UCase(SHORTCUTMENUFILE) & ThisWorkbook.Path & vbCrLf & vbCrLf & _
"You may need to reinstall BudgetMan", vbCritical, APPNAME
End Sub
Buttons and Icons in the MsgBox Function
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.
Call MsgBox function by using its parameter name
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
Check MsgBox result
"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
determines which button was pressed.
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
Each of the available buttons will produce an integer result.
Constant Value Description
vbOK 1 OK
vbCancel 2 Cancel
vbAbort 3 Abort
vbRetry 4 Retry
vbIgnore 5 Ignore
vbYes 6 Yes
vbNo 7 No
Getting a response from a message box with Select Case
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
Keep the message box visible while the user works with other open applications
Sub modelMsg()
MsgBox "How are you?", vbOKOnly + vbSystemModal, "System Modal"
End Sub
MsgBox"s third parameter is the message box"s title. Its fourth and fifth parameters are the Help file and context ID
Sub m()
msgBox "This is a Message", vbInformation, "This is a Title"
End Sub
MsgBox "Welcome to VBA", vbYesNoCancel, "VBA Message Box"
Sub yesNoSub()
msgBox "Welcome to VBA", vbYesNoCancel, "VBA Message Box"
End Sub
MsgBox with parameter Name: title:=myTitle, prompt:=question, buttons:=myButtons, helpfile:= "HelpX.hlp",context:=55
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
MsgBox with three parameters
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
Ok To Overwrite
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
Pass single parameter to MsgBox
Sub Test1()
MsgBox "Please make sure that the printer is switched on"
End Sub
Returning Values from the MsgBox Function
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
Save the return value from MsgBox to a variable
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
Set default button for MsgBox
Sub defaultSub()
MsgBox "Do you want to proceed?", vbYesNo + vbQuestion + vbDefaultButton2
End Sub
Specifying a Help File for a Message Box
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
Specifying a Title for a Message Box
Sub title()
MsgBox("Do you want to delete this workbook?", vbYesNo _
+ vbCritical + vbDefaultButton2, "Delete Workbook 12.39")
End Sub
The message box displays Yes, No, and Cancel buttons
Sub MessageBoxFunction()
Dim intAnswer As Integer
intAnswer = msgBox("Are You Sure?", vbYesNoCancel + vbQuestion, _
"Please Respond")
End Sub
The MsgBox buttons argument"s settings
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
Use Chr() function to format MsgBox
Sub charMsgBox()
MsgBox "A." & Chr(13) & "B ""C"" D" & Chr(13) & "E." & Chr(13) & "F."
End Sub
use the MsgBox function result without using a variable
Sub GetAnswer2()
If MsgBox("Continue?", vbYesNo) = vbYes Then
" ...[code if Yes is clicked]...
Else
" ...[code if Yes is not clicked]...
End If
End Sub
Use & to link messages
Sub MyMessage()
MsgBox "A." & Chr(13) _
& "B ""C"" E" & Chr(13) _
& "F." & Chr(13) _
& "G."
End Sub
Using the MsgBox Function: MsgBox (prompt [, buttons] [, title], [, helpfile, context])
Sub msgSub()
MsgBox "The procedure is complete."
End Sub
"Formatting the Message Box
Sub msgBoxQuotation()
MsgBox "A ""B"" C."
End Sub
Using the MsgBox Function with Arguments
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
Using the Select Case Statement to check the MsgBox button clicked
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