Lesson 30 of 60 – InputBox in VBA
50%

InputBox in VBA

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.

Note: InputBox is useful when you want your VBA program to receive information directly from the user.

1. What is InputBox?

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.

2. Why Use InputBox?

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:

  • Name
  • Age
  • Marks
  • Course name
  • Student ID
  • Quantity

3. Basic InputBox Syntax

The basic syntax is:

InputBox(prompt)

Example:

InputBox("Enter your name:")

The prompt is the message displayed to the user.

4. InputBox with a String

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.

5. InputBox with a Number

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

6. Storing InputBox Value in a Variable

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.

7. InputBox for Student ID

InputBox can be used to collect a student ID.

Dim studentID As String

studentID = InputBox("Enter Student ID:")

MsgBox "Student ID: " & studentID

8. InputBox for Marks

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.

9. InputBox with Excel Cells

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.

10. InputBox with a Variable and Cell

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.

11. InputBox for Two Values

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

12. InputBox for Calculations

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

13. InputBox for Student Marks

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.

14. InputBox with a Title

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.

15. InputBox with a Default Value

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.

16. InputBox for Data Entry

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

17. InputBox and Concatenation

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.

18. InputBox and Multiple Cells

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

19. InputBox with If Statement

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.

20. InputBox for Grade Calculation

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

21. InputBox and User Cancellation

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

22. InputBox for Course Selection

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.

23. InputBox for Fee Amount

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.

24. Practical Student Entry Example

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."

25. InputBox and Variables

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

26. Common InputBox Mistakes

Beginners commonly make these mistakes:

  • Forgetting to store the returned value.
  • Using text input directly in numeric calculations.
  • Not checking for an empty input.
  • Using an incorrect variable data type.
  • Forgetting to convert numeric input when necessary.

For example:

marks = CDbl(InputBox("Enter marks:"))

27. Practical Confirmation Example

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.

28. Best Practices for InputBox

  • Use clear and simple prompts.
  • Use meaningful variable names.
  • Convert numeric input when required.
  • Check for empty input.
  • Keep the program easy to understand.
  • Test different user inputs.

Good InputBox design makes a VBA program easier for users to operate.

29. Complete InputBox Workflow

A typical InputBox workflow is:

  1. Declare a variable.
  2. Display an InputBox.
  3. Ask the user for information.
  4. Store the entered value.
  5. Convert the value if necessary.
  6. Use the value in the program.
  7. Display or save the result.
Dim name As String

name = InputBox("Enter your name:")

Range("A1").Value = name

MsgBox "Data saved successfully."

30. Complete Understanding of InputBox

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.

📌 Key Points

  • InputBox is used to receive information from the user.
  • The entered value can be stored in a variable.
  • InputBox normally returns the entered value as text.
  • Use functions such as CInt or CDbl when numeric conversion is required.
  • InputBox can write data directly to Excel cells.
  • InputBox can be combined with If statements and calculations.
  • Always provide clear prompts to the user.

🧠 Quick Quiz

Question: Which VBA function is used to ask the user to enter information?