Site icon Best Excel Tutorial

VBA MsgBox function

The VBA MsgBox function is used to display a message box with a specified message and various options, such as buttons and icons. This function is often used to provide information to the user or to prompt them for input.

Syntax of the MsgBox function

MsgBox(prompt, [buttons], [title], [helpfile], [context])

How to use the MsgBox function

MsgBox "Hello, this is a simple message box."
MsgBox "Do you want to continue?", vbYesNo + vbQuestion, "Confirmation"
MsgBox "The file was saved successfully.", vbInformation, "Success"

The MsgBox function returns an integer value, which indicates the button that the user clicked. For example, if the user clicks the “Yes” button, the MsgBox function will return the integer value vbYes.

You can use the return value of the MsgBox function to control the flow of your VBA code. For example, you could use the following code to display a message box and then take different actions depending on which button the user clicked:

Dim result As Integer
result = MsgBox(
"Do you want to save your changes?",
vbYesNo + vbQuestion, "Confirmation")
If result = vbYes Then
End If

Exit mobile version