Lesson 29 of 60 – MsgBox
48%

MsgBox in VBA

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.

Note: MsgBox displays a dialog box containing a message and can also display buttons, icons, a title, and return the button selected by the user.

1. What is MsgBox?

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.

2. Why Use MsgBox?

MsgBox can be used for many purposes in an Excel VBA program.

  • Display information
  • Show calculation results
  • Display warnings
  • Confirm an operation
  • Inform the user that a task is complete
  • Help test VBA code

3. Basic MsgBox Syntax

The simplest syntax is:

MsgBox "message"

Example:

MsgBox "Welcome to Excel VBA"

The message is displayed inside a dialog box.

4. MsgBox with a String

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.

5. MsgBox with a Number

MsgBox can also display numeric values.

Sub ShowMarks()

    Dim marks As Integer

    marks = 85

    MsgBox marks

End Sub

The message box displays 85.

6. MsgBox with a Calculation

You can directly display the result of a calculation.

Sub Calculate()

    MsgBox 100 + 200

End Sub

The result displayed is 300.

7. MsgBox with Variables

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.

8. MsgBox with Excel Cell 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.

9. MsgBox with Multiple Lines

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.

10. MsgBox with a Title

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.

11. MsgBox with an OK Button

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.

12. MsgBox with OK and Cancel

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.

13. Yes and No Buttons

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.

14. Yes, No and Cancel Buttons

The vbYesNoCancel constant displays three buttons.

Sub AskUser()

    MsgBox "Save changes?", vbYesNoCancel, "Save"

End Sub

The user can select Yes, No, or Cancel.

15. Information Icon

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.

16. Warning Icon

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.

17. Critical Error Icon

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.

18. Question Icon

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.

19. Combining Buttons and Icons

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.

20. Storing the MsgBox Result

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.

21. Checking the Yes Button

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

22. Checking the No Button

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

23. Checking OK and Cancel

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

24. MsgBox with Student Result

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

25. MsgBox for Data Validation

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

26. Common MsgBox Mistakes

Beginners may make some common mistakes when using MsgBox.

  • Forgetting quotation marks around text.
  • Using the wrong button constant.
  • Forgetting to store the returned result when a response is required.
  • Using the wrong comparison when checking the result.
  • Creating unnecessarily long messages.

27. Practical Confirmation Example

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

28. Best Practices for MsgBox

  • Keep messages short and clear.
  • Use meaningful titles.
  • Use appropriate icons.
  • Use confirmation buttons for important actions.
  • Store the result when user input is required.
  • Use MsgBox to provide useful feedback to the user.

29. Complete MsgBox Workflow

A typical MsgBox workflow is:

  1. Decide what message should be displayed.
  2. Write the MsgBox statement.
  3. Add buttons if user input is required.
  4. Add an appropriate icon.
  5. Add a meaningful title.
  6. Store the result when necessary.
  7. Use an If statement to respond to the user's choice.

30. Complete Understanding of MsgBox

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.

📌 Key Points

  • MsgBox means Message Box.
  • It is used to display messages to the user.
  • It can display text, numbers, calculations, and cell values.
  • It can display different button combinations.
  • It can display information, warning, critical, and question icons.
  • The MsgBox result can be stored in a variable.
  • vbYes, vbNo, vbOK, and vbCancel can be used to check user responses.
  • MsgBox is useful for validation and confirmation.
  • MsgBox is also useful when testing VBA programs.

🧠 Quick Quiz

Question: Which VBA function is used to display a message box?