InputBOX function

In this article, we will learn how to create Inputbox function in Microsoft Excel by using the VBA. Msgbox is totally different from Inputbox, Inputbox is used to feed some input by the user, and then it returns the value to programmer for the further action.

The Syntax of Inputbox in VBA:-

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

Prompt: – It is required string data type. It helps to show the message in the dialog box. It can be a string, number or a Boolean value.

Title: – It is optional variant data type. This option we use to define the heading of message box but if we omit this option then it gives default title “Input”.

Default: – It is optional variant data type. If we use this option then it works to display the specified value and if we omit this option then the text box is left empty.

Xpos (X-Axis): – It is an optional variant data type which is used to give the direction of the input dialog box.

Ypos (Y-Axis): – It is an optional argument which is used to give the direction of the input dialog box.

HelpFile:- It is an optional Variant data type. If the help file and HelpContextId is available then help button will appear in dialog box.

HelpContextID:- It is an optional variant data type. This context ID number we use to define the help topic in help file.

Type:- It is an optional variant data type. If we omit this option then dialog box returns text.

Below is the table lists that we use to passed in the type argument. It can be one or a sum of the values.

Value Meaning
0 A formula
1 A number
2 Text (a string)
4 A logical value (True or False)
8 A cell reference, as a Range object
16 An error value, such as #N/A
64 An array of values

To learn how to create input box, follow below given steps:-

  • Open Excel Sheet and Press the key Alt+F11 to open the VBE.
  • In the code window, type below mentioned code:-

Sub Name_Verification()
Dim name As String
name = InputBox(“Enter your name :”, “Name”)
If Len(name) = 0 Then ‘Checking if Length of name is 0 characters
MsgBox “Please enter a valid name!”, vbCritical
Else
MsgBox “Hello ” & name & ” welcome to Excel Tips.”
End If
End Sub

image 1

  • To run the Macro, press the key F5
  • You will get the Input box

image 2

  • Enter the name in Inputbox

image 3

If you will not enter the name in input box, you will get critical message.

image 4

In this way we can create Inputbox with the message in Microsoft Excel by using VBA code.

Previous Chapter Next Chapter

Leave a Reply