Learn about VBA Input box

0
77

Learn about VBA Input box

In VBA, an InputBox function allows you to prompt the user for input. This is particularly useful for getting values directly from users without needing to create a form. Here’s how you can use it:

Basic Syntax of InputBox

Dim userInput As String

userInput = InputBox(Prompt, Title, Default)

  • Prompt: The message displayed in the input box.
  • Title: The title of the input box window (optional).
  • Default: A default value that appears in the input field (optional).

Example 1: Simple Input Box

Sub simpleinputbox()

Dim userName As String

userName = InputBox("Please Enter Your Name: ", "User Name")

If userName <> "" Then
MsgBox "Hello, " & userName & "!"
Else
MsgBox "No Name Entered!"
End If

End Sub

 

Example 2: Using a Default Value

Sub inputboxwithdefault()

Dim age As String
age = InputBox("Please enter your age", "User age", "25")

If age <> "" Then
MsgBox "You are " & age & " Years Old"
Else
MsgBox "No entered a Value"
End If


End Sub

Excel file for Download

Example : get a user to

Example 3: Numeric Input and Validation

If you want to validate that the input is a number, you can use IsNumeric:

Example 4: Input Box with a Loop Until Valid Input

You can also loop until the user provides a valid input or cancels.

confirm a decision by typing “Yes” or “No” using an InputBox?

Use an InputBox to get user confirmation and check their input.

For better understanding we should read below article also.

  1. How can we use Dim String option or other variable
  2.  If then else function (use of this)

in dept understanding we can go for below link

  1. How we can use Application of Input box

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here