The InputBox function in VBA is used to ask the user to enter some information. It displays a small dialog box with a message and an input field.
The value entered by the user can be stored in a variable and then used in calculations, cell operations, data entry, or other VBA programs.
InputBox is a VBA function that displays a dialog box asking the user to enter a value.
For example:
InputBox("Enter your name:")
The user can type a name and click OK.
InputBox is used when a VBA program needs information from the user while the program is running.
For example, you can ask the user for:
The basic syntax is:
InputBox(prompt)
Example:
InputBox("Enter your name:")
The prompt is the message displayed to the user.
You can ask the user to enter text.
Dim name As String
name = InputBox("Enter your name:")
MsgBox name
The entered name is stored in the name variable.
InputBox returns the entered value as text. If you need to perform calculations, you can convert the value into a number.
Dim age As Integer
age = CInt(InputBox("Enter your age:"))
MsgBox age
The value entered by the user can be assigned to a variable.
Dim studentName As String
studentName = InputBox("Enter student name:")
MsgBox "Student: " & studentName
Here, studentName stores the value entered by the user.
InputBox can be used to collect a student ID.
Dim studentID As String
studentID = InputBox("Enter Student ID:")
MsgBox "Student ID: " & studentID
You can ask the user to enter marks.
Dim marks As Double
marks = CDbl(InputBox("Enter marks:"))
MsgBox "Marks = " & marks
The CDbl function converts the input into a Double value.
The value entered through InputBox can be written directly into an Excel cell.
Range("A1").Value = InputBox("Enter your name:")
If the user enters Rahul, cell A1 will contain Rahul.
You can first store the input in a variable and then write it to a cell.
Dim name As String
name = InputBox("Enter your name:")
Range("A1").Value = name
This approach makes the program easier to understand.
You can use more than one InputBox in the same program.
Dim name As String
Dim course As String
name = InputBox("Enter your name:")
course = InputBox("Enter your course:")
MsgBox name & " - " & course
InputBox can collect numbers that are then used in calculations.
Dim a As Double
Dim b As Double
Dim total As Double
a = CDbl(InputBox("Enter first number:"))
b = CDbl(InputBox("Enter second number:"))
total = a + b
MsgBox "Total = " & total
InputBox can be used to collect marks for a student.
Dim marks As Double
marks = CDbl(InputBox("Enter student marks:"))
Range("B2").Value = marks
The entered marks are stored in cell B2.
The InputBox function can also display a custom title.
Dim name As String
name = InputBox("Enter your name:", "Student Information")
MsgBox name
The second argument specifies the title of the dialog box.
You can provide a default value that appears automatically in the input field.
Dim city As String
city = InputBox("Enter your city:", "Location", "Aurangabad")
MsgBox city
The third argument provides the default value.
InputBox can be used to create a simple data-entry program.
Dim studentName As String
Dim course As String
studentName = InputBox("Enter student name:")
course = InputBox("Enter course name:")
Range("A2").Value = studentName
Range("B2").Value = course
The value returned by InputBox can be combined with other text using the & operator.
Dim name As String
name = InputBox("Enter your name:")
MsgBox "Welcome " & name
If the user enters Ravi, the message will display Welcome Ravi.
You can collect multiple values and store them in different cells.
Dim name As String
Dim age As Integer
name = InputBox("Enter your name:")
age = CInt(InputBox("Enter your age:"))
Range("A2").Value = name
Range("B2").Value = age
InputBox values can be used with conditions.
Dim marks As Double
marks = CDbl(InputBox("Enter marks:"))
If marks >= 40 Then
MsgBox "Pass"
Else
MsgBox "Fail"
End If
This program checks whether the entered marks are 40 or more.
InputBox can collect marks and help calculate a simple grade.
Dim marks As Double
marks = CDbl(InputBox("Enter marks:"))
If marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 60 Then
MsgBox "Grade B"
ElseIf marks >= 40 Then
MsgBox "Grade C"
Else
MsgBox "Fail"
End If
When the user clicks Cancel, the InputBox returns an empty string. For text input, you can check whether the returned value is empty.
Dim name As String
name = InputBox("Enter your name:")
If name = "" Then
MsgBox "No name was entered."
Else
MsgBox "Hello " & name
End If
InputBox can collect the name of a course from the user.
Dim course As String
course = InputBox("Enter course name:")
Range("C2").Value = course
MsgBox "Course saved successfully."
This can be useful in simple student-management projects.
You can ask the user to enter a fee amount and store it in Excel.
Dim fee As Double
fee = CDbl(InputBox("Enter fee amount:"))
Range("D2").Value = fee
The CDbl function converts the entered value into a Double.
The following example collects student information and places it into a worksheet.
Dim name As String
Dim course As String
Dim marks As Double
name = InputBox("Enter student name:")
course = InputBox("Enter course:")
marks = CDbl(InputBox("Enter marks:"))
Range("A2").Value = name
Range("B2").Value = course
Range("C2").Value = marks
MsgBox "Student information saved."
Using variables with InputBox makes the program easier to organize.
Dim studentName As String
Dim marks As Double
Dim result As String
studentName = InputBox("Enter student name:")
marks = CDbl(InputBox("Enter marks:"))
If marks >= 40 Then
result = "Pass"
Else
result = "Fail"
End If
MsgBox studentName & " - " & result
Beginners commonly make these mistakes:
For example:
marks = CDbl(InputBox("Enter marks:"))
InputBox and MsgBox can be used together in a simple program.
Dim name As String
name = InputBox("Enter your name:", "Student Entry")
If name = "" Then
MsgBox "No name entered."
Else
MsgBox "Welcome " & name & "!"
End If
Here InputBox collects the information and MsgBox displays the result.
Good InputBox design makes a VBA program easier for users to operate.
A typical InputBox workflow is:
Dim name As String
name = InputBox("Enter your name:")
Range("A1").Value = name
MsgBox "Data saved successfully."
The InputBox is an important VBA function for receiving information from users. It can be used with variables, Excel cells, calculations, conditions, and other VBA features.
For example:
Dim name As String
name = InputBox("Enter your name:", "Student Information")
If name = "" Then
MsgBox "No name entered."
Else
Range("A1").Value = name
MsgBox "Name saved successfully."
End If
This simple concept can later be used to build practical Excel VBA applications such as student data-entry systems, fee systems, marksheets, and report generators.
Question: Which VBA function is used to ask the user to enter information?