The MsgBox function in Excel VBA is used to display a message box with a specific message and buttons. Here’s a guide on how to use the MsgBox function in your VBA code
Syntax
The syntax for the MsgBox function in Excel VBA is as follows:
MsgBox(prompt, buttons, title, helpfile, context)
Parameters
1.-prompt (required): The message you want to display in the message box. It can be a string or a numeric expression.
2.-buttons (optional): Specifies the buttons to display in the message box. It can be one of the following values:
vbOKOnly (default): 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 a critical message icon.
vbQuestion: Displays a question mark icon.
vbExclamation: Displays an exclamation point icon.
vbInformation: Displays an information icon.
3.- title (optional): The title or caption for the message box. It can be a string.
4.– helpfile (optional): The Help file to be used with the message box. It can be a string representing the name of the Help file.
5.-context (optional): The context number associated with the Help topic. It can be a numeric expression.
The MsgBox function returns a value that represents the button clicked by the user. The commonly used return values are:
vbOK: OK button was clicked.
vbCancel: Cancel button was clicked.
vbYes: Yes button was clicked.
vbNo: No button was clicked.
vbRetry: Retry button was clicked.
vbIgnore: Ignore button was clicked.
Examples
Button vbOKOnly
Here’s an example of using the button vbOKOnly :
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbOKOnly buttonresponse = MsgBox("This action cannot be undone. Are you sure you want to proceed?", vbOKOnly + vbExclamation, "Confirmation")Ifresponse = vbOKThenMsgBox"Action executed."End IfEnd Sub
In this example, a message box is displayed with an exclamation icon, a prompt asking the user if they want to proceed, and only the OK button using the vbOKOnly option.
If the user clicks OK, a separate message box with the text “Action executed” will be displayed.
Button vbOKCancel
Here’s an example of using the button vbOKCancel:
SubDisplayMessage()Dim response As VbMsgBoxResultresponse = MsgBox("Hello, this is a message box!", vbOKCancel + vbInformation, "Important Message")Ifresponse = vbOKThen' Perform action if OK is clickedElseIfresponse = vbCancelThen' Perform action if Cancel is clickedEnd IfEnd Sub
In this example, a message box is displayed with the message “Hello, this is a message box!” and OK/Cancel buttons. The response from the user is stored in the variable response, and different actions can be performed based on the clicked button.
Button vbAbortRetryIgnore
Here’s an example of using the button vbAbortRetryIgnore :
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbAbortRetryIgnore buttonsresponse = MsgBox("An error occurred. Would you like to retry?", vbAbortRetryIgnore + vbCritical, "Error")Ifresponse = vbAbortThenMsgBox"Operation aborted."ElseIfresponse = vbRetryThenMsgBox"Retrying operation..."' Perform retry operationElseIfresponse = vbIgnoreThenMsgBox"Operation ignored."End IfEnd Sub
In this example, a message box is displayed with a critical icon, a prompt asking the user if they want to retry after an error occurred, and three buttons: Abort, Retry, and Ignore, using the vbAbortRetryIgnore option.
Depending on the user’s choice, different actions will be performed. If the user clicks Abort, a separate message box with the text “Operation aborted” will be displayed. If the user clicks Retry, a message box saying “Retrying operation…” will be displayed, and you can perform the retry operation as needed. If the user clicks Ignore, a message box with the text “Operation ignored” will be displayed.
Button vbYesNoCancel
Here’s an example of using the button vbYesNoCancel:
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbYesNoCancel buttonsresponse = MsgBox("Do you want to save changes?", vbYesNoCancel + vbQuestion, "Save Changes")Ifresponse = vbYesThenMsgBox"Changes saved."ElseIfresponse = vbNoThenMsgBox"Changes not saved."ElseMsgBox"Operation canceled."End IfEnd Sub
In this example, a message box is displayed with a question icon, a prompt asking the user if they want to save changes, and three buttons: Yes, No, and Cancel, using the vbYesNoCancel option.
Depending on the user’s choice, different actions will be performed. If the user clicks Yes, a separate message box with the text “Changes saved” will be displayed. If the user clicks No, a message box saying “Changes not saved” will be displayed. If the user clicks Cancel, a message box with the text “Operation canceled” will be displayed.
Button vbYesNo
Here’s an example of using the button vbYesNo:
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbYesNo buttonsresponse = MsgBox("Do you want to proceed?", vbYesNo + vbQuestion, "Confirmation")Ifresponse = vbYesThenMsgBox"Proceeding with the operation."' Perform operationElseMsgBox"Operation canceled."End IfEnd Sub
In this example, a message box is displayed with a question icon, a prompt asking the user if they want to proceed, and two buttons: Yes and No, using the vbYesNo option.
If the user clicks Yes, a separate message box with the text “Proceeding with the operation” will be displayed, and you can perform the operation accordingly. If the user clicks No, a message box with the text “Operation canceled” will be displayed.
Button vbRetryCancel
Here’s an example of using the button vbRetryCancel:
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbRetryCancel buttonsresponse = MsgBox("Connection failed. Retry?", vbRetryCancel + vbExclamation, "Connection Error")Ifresponse = vbRetryThenMsgBox"Retrying connection..."' Perform retry operationElseMsgBox"Connection canceled."End IfEnd Sub
In this example, a message box is displayed with an exclamation icon, a prompt asking the user if they want to retry after a connection failure, and two buttons: Retry and Cancel, using the vbRetryCancel option.
If the user clicks Retry, a message box saying “Retrying connection…” will be displayed, and you can perform the retry operation as needed. If the user clicks Cancel, a separate message box with the text “Connection canceled” will be displayed.
Button vbCritical
Here’s an example of using the button vbCritical:
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbCritical buttonresponse = MsgBox("An unexpected error occurred. Please contact support.", vbCritical + vbOKOnly, "Error")Ifresponse = vbOKThenMsgBox"Thank you for reporting the error."End IfEnd Sub
In this example, a message box is displayed with a critical icon, a prompt informing the user about an unexpected error, and only the OK button using the vbOKOnly option.
If the user clicks OK, a separate message box with the text “Thank you for reporting the error” will be displayed.
Button vbQuestion
Here’s an example of using the button vbQuestion:
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbQuestion buttonresponse = MsgBox("Are you sure you want to delete this item?", vbQuestion + vbYesNo, "Confirmation")Ifresponse = vbYesThenMsgBox"Item deleted successfully."' Perform deletion operationElseMsgBox"Deletion canceled."End IfEnd Sub
In this example, a message box is displayed with a question icon, a prompt asking the user if they are sure they want to delete an item, and two buttons: Yes and No, using the vbYesNo option.
If the user clicks Yes, a separate message box with the text “Item deleted successfully” will be displayed, and you can perform the deletion operation accordingly. If the user clicks No, a message box with the text “Deletion canceled” will be displayed.
Button vbExclamation
Here’s an example of using the button vbExclamation:
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbExclamation buttonresponse = MsgBox("You have unsaved changes. Do you want to save them?", vbExclamation + vbYesNoCancel, "Unsaved Changes")Ifresponse = vbYesThenMsgBox"Changes saved."' Perform save operationElseIfresponse = vbNoThenMsgBox"Changes discarded."' Discard changesElseMsgBox"Operation canceled."End IfEnd Sub
In this example, a message box is displayed with an exclamation icon, a prompt informing the user about unsaved changes, and three buttons: Yes, No, and Cancel, using the vbYesNoCancel option.
If the user clicks Yes, a separate message box with the text “Changes saved” will be displayed, and you can perform the save operation accordingly. If the user clicks No, a message box with the text “Changes discarded” will be displayed, and you can discard the changes. If the user clicks Cancel, a message box with the text “Operation canceled” will be displayed.
Button vbInformation
Here’s an example of using the button vbInformation:
SubDisplayMessage()Dim response As VbMsgBoxResult' Example with vbInformation buttonresponse = MsgBox("Congratulations! Your registration was successful.", vbInformation + vbOKOnly, "Registration")Ifresponse = vbOKThenMsgBox"Thank you for registering."End IfEnd Sub
In this example, a message box is displayed with an information icon, a message informing the user about a successful registration, and only the OK button using the vbOKOnly option.
If the user clicks OK, a separate message box with the text “Thank you for registering” will be displayed.