This is a built in dialog box that can be used to display a message to the user and wait for a response.
This function is extremely useful and is often very handy when you are debugging.
This function returns a value which indicates what choice the user made.
This function can be inserted anywhere in your code to pause the program and display a message to the user.
Displaying a Question
This function is typically used to as a way of asking the user a question.
If MsgBox("save the file ?",VBA.vbMsgBoxStyle.vbYesNo) = VBA.vbMsgBoxResult.vbYes Then
'save the file
Displaying a Message
This function csn also be used by ignoring the answer to just display a message to the user ignoring the returned value.
When you are not interesting in the return value it is better to use the Call keyword.
Call MsgBox("This file has been saved",VBA.vbMsgBoxStyle.vbOKOnly)
Call MsgBox("This file has been saved") 'vbOkOnly is the default
iResult = MsgBox(prompt[, buttons] [, title] [, helpfile, context])
|prompt||Required. String expression containing the message to be displayed. The maximum length of a single line prompt is approximately 1024 characters, depending on the width of the characters used. You can create a multi line message by using the carriage return character vbCrLf (Chr(13)), a linefeed character (Chr(10)), or carriage return-linefeed character combination (Chr(13) & Chr(10)) between each line.|
|buttons||Optional. Long numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If omitted, the default value is 0, which is a VbOkOnly button|
|title||Optional. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.|
|helpfile||Optional. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If helpfile is provided, context must also be provided.|
|context||Optional. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If context is provided, helpfile must also be provided.|
If the Esc key is pressed then this is the same as the Cancel button being pressed.
VBA.vbMsgBoxStyle - Button and Icon Setttings
|vbOKOnly||0||Display OK button only (this is the default).|
|vbOKCancel||1||Display OK and Cancel buttons.|
|vbAbortRetryIgnore||2||Display Abort, Retry, and Ignore buttons.|
|vbYesNoCancel||3||Display Yes, No, and Cancel buttons.|
|vbYesNo||4||Display Yes and No buttons.|
|vbRetryCancel||5||Display Retry and Cancel buttons.|
|vbCritical||16||Display Critical Message icon.|
|vbQuestion||32||Display Warning Query icon.|
|vbExclamation||48||Display Warning Message icon.|
|vbInformation||64||Display 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.|
VBA.vbMsgBoxStyle - Modality
|vbApplicationModal||0||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||Adds a 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||Specifies text should appear as right-to-left reading on Hebrew and Arabic systems|
Type Mismatch Error
Be careful when using this function as you can easily get a type mismatch when this line of code is executed.
The following line will compile however when it tries to execute it your code will be interupted with a "Type Mismatch" error.
Call MsgBox("Press any key to continue", "Macro Interrupted")
There is no compile time checking of the arguments passed to this function so make sure you are passing the correct datatype for each of the arguments.
The correct line of code should have been:
Call MsgBox("Press any key to continue", , "Macro Interrupted")