Dialog Boxes in VBA

Dialog Boxes in VBA

In GUI (Graphical User Interface) programming dialog box is a common type of window. It may display information or may be asked for input.

In VBA there are two types of Dialog Box.

  1. MsgBox
  2. InputBox

1. MsgBox() in VBA

The MsgBox function displays a dialog box call message box and it will display information and waits for the user action to click a button, click button action is performed as per the button clicked by the user. MsgBox() have two types of syntax.

First Type MsgBox in VBA Video in Hindi


  • First type Syntax of MsgBox in VBA
MsgBox(textMsg)

Where…
textMsg: A string which displayed as a message in the dialog box.

Example:1 – MsgBox() in VBA

Private Sub cmdBttnType1_Click()
    MsgBox ("This is type 1 msgBox")
End Sub

Output :

Example MsgBox in MS Excel VBA

Example:2 – MsgBox() in VBA

Private Sub cmdBttnType1_Click()
    Dim var1 As String
    var1 = "This is type 1 msgBox"
    MsgBox (var1)
End Sub

In the above example text which we want to display is written between double quote (“”) because it is a string.

Output :

Example MsgBox in MS Excel VBA

Second Type MsgBox in VBA Video in Hindi


  • Second type Syntax of MsgBox in VBA
variableName = MsgBox(textMsg, [Buttons], [Title])

Where…

textMsg: A string which displayed as a message in the dialog box.
Buttons: Optional Parameter. it is A Numeric expression, which provide choice of buttons (yes, no, cancel, etc.)
Title: Optional Parameter. A String expression displayed in the title bar of the dialog box

If we use “Button” or “Title” Parameter, then MsgBox() return button value so that we have to store that returned value in variable

List of Button constant value

List of Button constant value of MsgBox in MS Excel VBA

List of Icon constant

We can also display a different icon in the message box. For that we have to use icon constant with the button. To use icon constant, we have to use write button constant then put plus “+” symbol and then write icon constant.

variableName = MsgBox(textMsg, [buttonConstant + iconConstant], [Title])

there is a different constant value for a different icon that is as under. Icon Constant is value and icon Display as per Description.

List of Icon constant value of MsgBox in MS Excel VBA

Value Return by MsgBox()

As per the button click by user MsgBox return numeric value accordingly. Below table shows Button, its value and its constant for that value.

List of value return by MsgBox in MS Excel VBA

Example:3 MsgBox() in VBA

Private Sub cmdBttnType2_Click()
    Dim i As Integer
    
    i = MsgBox("This is Type 2 MsgBox", vbYesNo + vbCritical, "Type2")
    
End Sub

Output :

Example MsgBox in MS Excel VBA

In the above example the Yes-No button and the critical icon will display. If the user press the “Yes” button then as per the value table MsgBox() return 1 or vbOk constant. If the user press the “No” button then asper value table MagBox() return 7 or vbNo constant.

2. InputBox()

InputBox asks the user to enter a value in a dialog box.

The InputBox function prompts the dialog box that asks the users to enter values. After entering the values, if the user clicks the OK button or presses ENTER key, the InputBox function will return the text which is inputted by the user in the text box. If the user clicks the Cancel button, the function will return an empty string (“”).

InputBox in VBA Video in Hindi


Syntax of InputBox in VBA

stingVariableName = InputBox(txtMsg ,[title],[defaultValue])

Where…

txtMsg: A string which displayed as a message in the dialog box.
title: Optional Parameter. A String expression displayed in the title bar of the dialog box.
defaultValue: Optional Parameter. A default text which is displayed in the text box of Dialog box.

Example of InputBox in VBA:

Write below code into click event of command button.

Private Sub cmdBttnCity_Click()
    Dim city As String
    city = InputBox("Enter You City Name:", "Enter City", "Mumbai")
    MsgBox (city)
End Sub

Output:

Example of InputBox in MS Excel VBA
Example of InputBox in MS Excel VBA

Leave a Reply

Your email address will not be published.

*