The MsgBox function is used to display a message box to the user in VBA. It is one of the simplest ways to show information, warnings, questions, and results from a VBA program.
MsgBox is very useful for beginners because it allows you to immediately see the result of your VBA code.
MsgBox stands for Message Box. It is used to display a message to the user.
Basic example:
Sub ShowMessage()
MsgBox "Hello VBA"
End Sub
When the procedure runs, Excel displays a message box containing Hello VBA.
MsgBox can be used for many purposes in an Excel VBA program.
The simplest syntax is:
MsgBox "message"
Example:
MsgBox "Welcome to Excel VBA"
The message is displayed inside a dialog box.
You can display a String variable using MsgBox.
Sub ShowName()
Dim studentName As String
studentName = "Rahul"
MsgBox studentName
End Sub
The message box displays the value stored in studentName.
MsgBox can also display numeric values.
Sub ShowMarks()
Dim marks As Integer
marks = 85
MsgBox marks
End Sub
The message box displays 85.
You can directly display the result of a calculation.
Sub Calculate()
MsgBox 100 + 200
End Sub
The result displayed is 300.
Variables can be combined with text in a message.
Sub StudentMessage()
Dim marks As Integer
marks = 85
MsgBox "Student Marks: " & marks
End Sub
The & operator joins the text and the variable value.
You can display the value stored in an Excel cell.
Sub ShowCellValue()
MsgBox Range("A1").Value
End Sub
The value from cell A1 is displayed in the message box.
The vbCrLf constant can be used to create a new line in a message.
Sub StudentDetails()
MsgBox "Name: Rahul" & vbCrLf & _
"Course: ADCA" & vbCrLf & _
"Marks: 85"
End Sub
Each piece of information appears on a separate line.
You can specify a title for the message box.
Sub ShowTitle()
MsgBox "Welcome to VBA", , "My VBA Program"
End Sub
The third argument specifies the title displayed in the dialog box.
The vbOKOnly constant displays only an OK button.
Sub ShowInformation()
MsgBox "Data saved successfully", vbOKOnly, "Information"
End Sub
The user can click OK to close the message box.
The vbOKCancel constant displays OK and Cancel buttons.
Sub ConfirmAction()
MsgBox "Do you want to continue?", vbOKCancel, "Confirmation"
End Sub
The user can choose either OK or Cancel.
The vbYesNo constant displays Yes and No buttons.
Sub AskQuestion()
MsgBox "Do you want to continue?", vbYesNo, "Question"
End Sub
This is useful when the user needs to choose between two options.
The vbYesNoCancel constant displays three buttons.
Sub AskUser()
MsgBox "Save changes?", vbYesNoCancel, "Save"
End Sub
The user can select Yes, No, or Cancel.
The vbInformation constant displays an information icon.
Sub ShowInfo()
MsgBox "File saved successfully", _
vbInformation, _
"Information"
End Sub
Information icons are useful for normal informational messages.
The vbExclamation constant displays an exclamation or warning icon.
Sub ShowWarning()
MsgBox "Please check the entered marks", _
vbExclamation, _
"Warning"
End Sub
This can be used to draw attention to an important message.
The vbCritical constant displays a critical message icon.
Sub ShowError()
MsgBox "Unable to process the request", _
vbCritical, _
"Error"
End Sub
It can be used when displaying an important error message.
The vbQuestion constant displays a question icon.
Sub AskUser()
MsgBox "Do you want to delete this record?", _
vbYesNo + vbQuestion, _
"Confirm"
End Sub
It is commonly used for confirmation questions.
You can combine a button style and an icon using the + operator.
Sub ConfirmDelete()
MsgBox "Delete this record?", _
vbYesNo + vbQuestion, _
"Confirm Delete"
End Sub
This creates a message box with Yes and No buttons and a question icon.
MsgBox can return a value representing the button selected by the user.
Sub CheckAnswer()
Dim answer As VbMsgBoxResult
answer = MsgBox("Continue?", vbYesNo, "Question")
End Sub
The variable answer stores the result returned by MsgBox.
You can check whether the user selected the Yes button.
Sub CheckAnswer()
Dim answer As VbMsgBoxResult
answer = MsgBox("Continue?", vbYesNo, "Question")
If answer = vbYes Then
MsgBox "You selected Yes"
End If
End Sub
You can also check whether the user selected No.
Sub CheckAnswer()
Dim answer As VbMsgBoxResult
answer = MsgBox("Continue?", vbYesNo, "Question")
If answer = vbNo Then
MsgBox "You selected No"
End If
End Sub
The result can also be checked when using OK and Cancel buttons.
Sub CheckAction()
Dim answer As VbMsgBoxResult
answer = MsgBox("Save the record?", vbOKCancel, "Save")
If answer = vbOK Then
MsgBox "Record saved"
ElseIf answer = vbCancel Then
MsgBox "Operation cancelled"
End If
End Sub
MsgBox is useful for displaying student results.
Sub StudentResult()
Dim marks As Integer
marks = Range("B2").Value
If marks >= 40 Then
MsgBox "Student Passed", _
vbInformation, _
"Result"
Else
MsgBox "Student Failed", _
vbExclamation, _
"Result"
End If
End Sub
MsgBox can inform users when entered data is invalid.
Sub ValidateMarks()
Dim marks As Integer
marks = Range("B2").Value
If marks < 0 Or marks > 100 Then
MsgBox "Enter marks between 0 and 100", _
vbExclamation, _
"Invalid Marks"
End If
End Sub
Beginners may make some common mistakes when using MsgBox.
Let's create a confirmation message before deleting a student record.
Sub DeleteStudent()
Dim answer As VbMsgBoxResult
answer = MsgBox( _
"Do you want to delete this student?", _
vbYesNo + vbQuestion, _
"Delete Student")
If answer = vbYes Then
MsgBox "Student record deleted", _
vbInformation, _
"Completed"
Else
MsgBox "Operation cancelled", _
vbInformation, _
"Cancelled"
End If
End Sub
A typical MsgBox workflow is:
The MsgBox function is used to communicate information between a VBA program and the user. It can display messages, calculations, warnings, questions, and confirmation dialogs.
MsgBox can contain different button combinations and icons. It can also return the button selected by the user, allowing the VBA program to make decisions based on that response.
Learning MsgBox is an important step toward creating interactive Excel VBA applications.
Question: Which VBA function is used to display a message box?