×

Excel VBA InputBox

Input Box

The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks the OK button (After entering the values) or presses ENTER on the keyboard, this function will return the specific text in the text box. Else, if the user presses the Cancel button or the box is empty, by default, the InputBox function will return an empty string ("").

Syntax

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

Parameter

  • Prompt (required) – This parameter represents a string value shown as a message in the dialog box. The maximum number of characters that can be entered is approximately 1024. If the specified message is more than a line, then the lines can be separated using a carriage return character (Chr(13)) or a linefeed character (Chr(10)) between each line.
  • Title (optional) – This parameter represents a String expression shown in the title bar of the dialog box. If you don’t specify anything, by default it will show the name of the application.
  • Default (optional) – It represents a default text that the user would like to be displayed.
  • XPos (optional) – This parameter represents the position of the X-axis (the distance from the left side of the screen horizontally). If XPos is not specified, by default, the input box is horizontally centered.
  • YPos (optional) – This parameter represents the position of the Y-axis (the distance from the left side of the screen vertically). If YPos is not specified, by default, the input box is vertically centered.
  • Helpfile (optional) – This parameter represents a string expression that identifies the helpfile to provide context-sensitive Help for the dialog box.
  • Context (optional) - This parameter represents a numeric expression that defines the Help context number assigned by the Help author to the appropriate Help topic. If the context is specified, helpfile should also be specified.

Example 1

Sub InputBox_Exercise()
 'prompt a box so the users can enter values
 IB = InputBox("Enter your name here!", "Identity", "John Smith")
 'printing the value at Range A1
 Range("A1") = IB
 End Sub 

Output

Press F5 to run the VBA code.

Press F5 to run the VBA code

Type the text in the prompt box and click on ok.

Type the text in the prompt box

You will notice, the name has been displayed in the excel sheet at A1 cell.

name has been displayed in the excel sheet at A1 cell

Example 2: Write a macro demonstrating the use of Inputbox and MessageBox collectively.

Sub Session_Msgbox_InputBox()
 Dim CR As Byte
 CR = ActiveCell.Row
 col = 2
 'taking the name from the user
 IB = InputBox("Enter your name here!", "Identity", "John Smith")
 'passing the name in the msgbox and asking the user 
 'whether he wants to add 100 or not
 Add_Sales = MsgBox("Hello " + IB + "! Do you want to add 100 to the current sales?", vbYesNo)
     'if user press No, then we will terminate the program
     If Range("A" & CR).Value = Empty Then Exit Sub
     'if user press yes then we will add 100
     If Add_Sales = vbYes Then
         Set rng = Range("D2:D12") 
         For Each cell In rng
             Range("E" & col) = cell.Value + 100
             col = col + 1
             'MsgBox "100 has been added to the current row's sales amount"
         Next
     End If
 End Sub 

Output

Press F5 to run the output. The InputBox will pop up as shown below.

InputBox will pop up as shown

Type your name and click on ok. The MsgBox will pop up as shown below.

values have been displayed in the excel sheet

Click on yes. You will notice, the values have been displayed in the excel sheet.

values have been displayed in the excel sheet


Related Topics

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

VBA Type Mismatch Error

What is a Type Mismatch Error? VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched...

3 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

Excel VBA- Pivot Table Fields

VBA- Pivot Table Fields: The Pivot Fields collection contains all the fields from the data source, including any calculated fields. The main aspect of adding a field is its Position...

4 minutes read.

Debugging in Excel VBA

Debugging in VBA: Debugging is a technique used to fix errors in programming languages. In Excel VBA, we have different ways by which you can identify the error in the...

2 minutes read.

Excel VBA Space Function

VBA Space Function: The Space function in VBA creates a String consisting of a specified number of spaces. Syntax Space (Number) Parameter Number (required) - This parameter represents the number of spaces. Return This function returns a...

1 minute read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel VBA DateAdd Function

The DateAdd function in VBA adds a time interval to a supplied date and/or time and returns the resultant date/time. Syntax Dateadd (Interval, Number, Date) Parameter Interval (required) – This parameter a string specifying the interval to be...

1 minute read.

Excel VBA IsMissing Function

VBA IsMissing Function: The IsMissing function in VBA checks if any parameter to a procedure is missing or not. It returns a Boolean value True if the specified parameter has not been...

1 minute read.

VBA Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

5 minutes read.

Excel VBA CDec Function

VBA CDec Function: The CDec function in VBA converts an expression into a Decimal data type. Syntax CDec (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA StrComp Function

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes read.

Excel VBA MonthName Function

Excel VBA MonthName Function: The MonthName function in VBA returns a string with the month name for the specified month number. Syntax MonthName (Month, [Abbreviate]) Parameter Month (required) – This parameter represents an integer between...

1 minute read.

If ElseIf ElseIf Statement or Nested If statement in VBA

VBA Excel: If … ElseIf … ElseIf Statement or Nested If statement This function enables you to check multiple conditions and, based on that, then run one of the statement blocks present. If...

2 minutes read.

Excel VBA CVar Function

VBA CVar Function: The CVar function in VBA converts an expression into a Variant data type. Syntax CVar (Expression) Parameter Expression (required) – This parameter represents the expression that that you want to convert...

1 minute read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

1 minute read.

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

Excel VBA String Function

The String function in VBA creates a String, consisting of several repeated characters. Syntax String (Number, Character) Parameter Number (required) – This parameter represents the number of characters in the returned String. Character (required) – This parameter...

1 minute read.

VBA Subscript out of Range

What is Subscript out of Range? The VBA Subscript out of Range error (which is also called as Run-Time Error 9) mostly triggers when the user selects any cell, sheet, or workbook which does...

5 minutes read.

Excel VBA Asc Function

VBA Asc Function: The Asc function in VBA returns an integer showing the character code for the first character of a given string. Syntax Asc (String) Parameter String (required) – This parameter represents the text...

1 minute read.