In VBA (Visual Basic for Applications), the `MsgBox` function is used to display a message box to the user and optionally get a response. The message box can display a message, a title, buttons, and an icon. Here’s how to use the `MsgBox` function:
MsgBox(prompt, [buttons], [title], [helpfile], [context])
- `prompt`: The text displayed in the message box. This is the only required parameter.
- `buttons`: Optional. Specifies the buttons, icon, default button, and modality of the message box. It’s usually specified using constants (e.g., `vbYesNoCancel`, `vbInformation`, `vbExclamation`).
- `title`: Optional. The text displayed in the title bar of the message box. If omitted, the application name is used.
- `helpfile`: Optional. The name of the help file to use to provide context-sensitive help for the dialog box.
- `context`: Optional. The context ID number of the help topic within the help file specified in `helpfile`.
Here’s a simple example of using the `MsgBox` function:
Sub ShowMessage()
Dim response As Integer
response = MsgBox("Do you want to continue?", vbYesNo + vbQuestion, "Continue Confirmation")
If response = vbYes Then
MsgBox "User chose Yes"
ElseIf response = vbNo Then
MsgBox "User chose No"
End If
End Sub
In this example, we:
- Display a message box with the Yes and No buttons and a question mark icon. The text “Do you want to continue?” will appear in the message box, and “Continue Confirmation” will be the title of the message box.
- Capture the user’s response in the `response` variable.
- Based on the user’s choice (Yes or No), we display another message indicating the user’s choice.
Here are some of the commonly used constants for the `buttons` argument to define which buttons and icons appear in the message box:
- `vbOKOnly`: Displays only the OK button.
- `vbOKCancel`: Displays OK and Cancel buttons.
- `vbAbortRetryIgnore`: Displays Abort, Retry, and Ignore buttons.
- `vbYesNoCancel`: Displays Yes, No, and Cancel buttons.
- `vbYesNo`: Displays Yes and No buttons.
- `vbRetryCancel`: Displays Retry and Cancel buttons.
- `vbCritical`: Displays the Critical Message icon.
- `vbQuestion`: Displays the Warning Query icon.
- `vbExclamation`: Displays the Warning Message icon.
- `vbInformation`: Displays the Information Message icon.
- `vbDefaultButton1`: First button is default.
- `vbDefaultButton2`: Second button is default.
- `vbDefaultButton3`: Third button is default.
- `vbApplicationModal`: Application modal; the user must respond to the message box before continuing work in the current application.
- `vbSystemModal`: System modal; all applications are suspended until the user responds to the message box.
These constants can be combined using the `+` operator. For instance, `vbYesNo + vbQuestion` will display a message box with Yes and No buttons and a question mark icon.
Remember, `MsgBox` can return various values depending on the buttons pressed by the user. For example, if you use `vbYesNo`, the function will return `vbYes` if the user clicks Yes and `vbNo` if the user clicks No.