How to Create a Dialog Box in Excel – 3 Examples

 

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.

Example 1 to create a dialog box in excel

  • Enter the following code in the module.
Sub Simplebox()
MsgBox “Hello Altruists!”
End Sub
  • Press F5 to run the code.

Example 1 to create a dialog box in excel

  • This is the output.

Example 1 to create a dialog box in excel

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

Example 1 to create a dialog box in excel

  • Run the code by pressing F5.
  • A new window will be displayed.

Example 2 to create a dialog box in excel

  • If you click Yes, the window will show:

Example 2 to create a dialog box in excel

  • If you click No, the window will show:

Example 2 to create a dialog box in excel

  • 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.

Example 2 to create a dialog box in excel

  • If you click Yes, the window shows:

Example 2 to create a dialog box in excel

  • If you click No, the window shows:

Example 2 to create a dialog box in excel

  • 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

Example 2 to create a dialog box in excel

  • Run the code by pressing f5.
  • The dialog box displays the heading YES or NO.

Example 2 to create a dialog box in excel

  • Clicking Yes, the window shows: YES PLEASE.

Example 2 to create a dialog box in excel

  • Clicking No, the window shows: NO THANKS.

Example 2 to create a dialog box in excel

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.

Read More: How to Input Dialog Box in Excel VBA 


Things to remember

Select macro-enabled extensions.


Download Practice Workbook

Download the practice workbook.


Related Articles


<< Go Back to Excel PartsLearn Excel

Get FREE Advanced Excel Exercises with Solutions!
Souptik Roy
Souptik Roy

Souptik Roy, a BSc graduate in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology, dedicated 1.5 years to the ExcelDemy project. During this time, he authored 50+ articles and reviewed 20+ for ExcelDemy. Presently, he is a designer and content developer at YouHaveGotThisMath and Brainor, sister concerns of ExcelDemy. His educational content spans science, mathematics, and grammar. Roy's interests include developing creative ideas, visualizing concepts with tools like Adobe Illustrator, and problem-solving within Microsoft... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo