A dialog box is shown below.
Example 1: Create a Simple Message Box
Steps:
- Open the Microsoft Visual Basic for Application by pressing Alt+f11.
- Select Insert.
- Choose Module.
- Enter the following code in the module.
Sub Simplebox()
MsgBox “Hello Altruists!”
End Sub
- Press F5 to run the code.
- This is the output.
Read More: Dialog Box Launcher in Excel
Example 2 – Create an Interactive Dialog Box
Steps:
- Enter the following code in the VBE.
Sub TestInputBox()
Dim R As Integer
R = MsgBox("Are you sure to press YES",vbYesNo)
If R = vbYes Then
MsgBox (“You Pressed YES”)
Else
MsgBox (“You Pressed NO”)
End If
End Sub
- Run the code by pressing F5.
- A new window will be displayed.
- If you click Yes, the window will show:
- If you click No, the window will show:
- To modify the interactive dialog box, use the following code:
Sub TestInputBox()
Dim R As Integer
R = MsgBox("Are you sure to press YES",vbYesNo + vbQuestion)
If R = vbYes Then
MsgBox “You Pressed YES”, vbInformation
Else
MsgBox “You Pressed NO”, vbCritical
End If
End Sub
- Click Run.
- This window will be displayed.
- If you click Yes, the window shows:
- If you click No, the window shows:
- To add a heading the dialog boxes, enter the following code.
Sub TestInputBox()
Dim R As Integer
R = MsgBox("Are you sure to press YES",vbYesNo + vbQuestion, “YES or NO”)
If R = vbYes Then
MsgBox “You Pressed YES”, vbInformation, “YES PLEASE”
Else
MsgBox “You Pressed NO”, vbCritical, “NO THANKS”
End If
End Sub
- Run the code by pressing f5.
- The dialog box displays the heading YES or NO.
- Clicking Yes, the window shows: YES PLEASE.
- Clicking No, the window shows: NO THANKS.
Read More: How to Close Dialog Box in Excel
Example 3 – Create a Dialog Box to Input Data in Excel
Steps:
- In the VBE, enter the following code:
Sub TestInputBox()
Dim R As String
R = InputBox("Write YES or NO", "YES or NO", "Enter your text HERE")
MsgBox R
End Sub
- Click Run.
- A window is displayed.
- Enter Yes in the input box.
- Click OK.
- The dialog box displays YES.
Things to remember
Select macro-enabled extensions.
Download the practice workbook.
Related Articles
- How to Open the Insert Function Dialog Box in Excel
- How to Stop Name Conflict Dialog Box in Excel
- [Solved!] Excel Dialog Box Hidden
- How to Display Excel Options Dialog Box
<< Go Back to Excel Parts | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!