VBA Inputbox Msgbox Function

VBA InputBox MsgBox Function are two commonly used functions in VBA programming. InputBox is used to get input from the user, while MsgBox is used to display a message in a dialog box

Here is an example of how to use InputBox to get input from the user and store it in a variable:

Dim Name as String
name = InputBox ("Enter your name here")

Here is an example of how to use MsgBox to display a message in a dialog box:

MsgBox "Welcome to VBA "

VBA – InputBox Function

The InputBox function prompts the users to enter values.

Syntax: -
        InputBox (prompt [, title] [, default] [, xpos] [, ypos] [, helpfile, context])

The String Variable with an Input Box

Sub name_input()
Dim name As String
name = InputBox ("what is your name", "Student Detail", "type here")
MsgBox "The student name is " & name
End Sub

The Number Variable with an Input Box

Sub age_input()
Dim Age As Integer
Age = InputBox ("what is your age", "Student Detail", "type here")
MsgBox "The student age is " & Age
End Sub

VBA MsgBox Function

A MsgBox is nothing but a dialog box that you can use to inform your users by showing a custom message. The MsgBox function displays a message box and waits for the user to click a button and then an action is performed based on the button clicked by the user.

Syntax:
                   MsgBox (prompt [, buttons] [, title] [, helpfile, context])

      MsgBox Buttons – vbOKOnly (Default)

      If you only use the prompt and don’t specify any of the arguments, you will get the default message box as shown below:

      Sub DefaultMsgBox()
      MsgBox "Welcome to excel VBA family of myitschools"
      End Sub

      MsgBox Buttons – OK & Cancel

      If you only want to show the OK and the Cancel button, you need to use the vbOKCancel constant.

      Sub MsgBoxOKCancel()
      MsgBox "Do you Want to Continue?", vbOKCancel
      End Sub

      MsgBox Buttons – Yes and No

      You can use the ‘vbYesNo’ constant to show the Yes and No buttons.

      Sub MsgBoxYesNo()
      MsgBox "Do you like cricket?", vbYesNo
      End Sub

      MsgBox Buttons – Yes, No and Cancel

      You can use the ‘vbYesNoCancel’ constant to show the Yes, No, and Cancel buttons.

      Sub MsgBoxYesNoCancel()
      MsgBox "Do you know about VBA msgbox?", vbYesNoCancel
      End Sub

      MsgBox Buttons – Abort, Retry, and Ignore

      You can use the ‘vbAbortRetryIgnore’ constant to show the Abort, Retry, and the Ignore buttons.

      Sub MsgBoxAbortRetryIgnore()
      MsgBox "What do you want to do?", vbAbortRetryIgnore
      End Sub

      MsgBox Buttons – Help Button

      You can use the ‘vbMsgBoxHelpButton’ constant to show the help button. You can use it with other button constants.

      Sub MsgBoxRetryHelp()
      MsgBox "What do you want to do next?", vbRetryCancel + vbMsgBoxHelpButton             
      End Sub

      MsgBox Icons – Critical

      If you want to show a critical icon in your MsgBox, use the vbCritical constant. You can use this along with other button constants (by putting a + sign in between the codes).

      For example, below is a code that will show the default OK button with a critical icon.

      Sub MsgBoxCriticalIcon ()
      MsgBox "Does there is any health issue?", vbCritical
      End Sub

      If you want to show the critical icon with Yes and No buttons, use the following code:

      Sub MsgBoxCriticalI()
      MsgBox "This is a sample box", vbYesNo + vbCritical
      End Sub
      Scroll to Top