VBA MODULE 12


INPUTBOX()


Input Box function displays a dialog box and prompts user to input a value. The input value can be number, text string, or a range address.

If user clicks Ok button after entering value, the InputBox will return a result as per the user input. If user clicks Cancel button, it will return empty string.

 

Syntax :

 

InputBox (prompt[,title][,default])

 

 

InputBox Argument Description

 

ARGUMENT

WHAT IT DOES

Prompt

It displays the text in the Inputbox. This is always required to run the Inputbox function.

Default

It displays default value in the field where user inputs his desired value. This argument is optional

Title

It displays the title of the Inputbox . This is also optional so when we leave it blank it takes default value as the application name

 

Let’s take a simple example where we only give the prompt Argument and for rest argument VBA will take the default value as discussed above.

Statement starting with the single inverted comma(‘) is just a comment to understand what we are doing and these lines are ignored when VBA executes the code.

 

Sub sampleInputBox()

'Declaring a variable to store the inputbox value

'Dim statement creates a String variable called YourAge

Dim YourAge As String

'Accepting the value from the user

YourAge = InputBox("What is your age?")

'Displaying the value stored in the variable using Message Box

MsgBox YourAge

End Sub

 

Here is above example with all the three arguments.

 

Sub sampleInputBox()

Dim YourAge As String

YourAge = InputBox("What is your age?", "My Age", "Enter your age")

MsgBox YourAge

End Sub

 

© Snagoff. All right reserved.