VBA For Beginners VBA Input Box Examples - Online PC Learning
VBA For Beginners VBA Input Box Examples - Online PC Learning
(http://www.onlinepclearning.com
(http://www.onlinepclearning.com/vba-beginners-vba-input-box-examples/)
OVERVIEW.
1. Syntax
2. Input Function
3. Input Method
4. Type argument
5. A Simple Input Box
6. VBA Examples
7. Create a User Login
1. Example 1 Add a name
2. Example 2 Create a login
3. Example 3 Create a login dynamic
4. Example 4 Login with 3 attempts (Download)
8. Collect employee age
1. Example 1 Age list
2. Example 2 Age calculator (Download)
9. BMI Calculator
1. Example Basic BMI Calculator (Download)
10. Working with Ranges
1. Example Dynamic print area
We can let the end users of our applications interact with the data flow with
Input Boxes. They are quick, effective and easy to construct and modify.
Putting this function into your box of tricks will add greater flexibility to your
work
(http://www.onlinepclearning.com/wp-content
/uploads/2013/10/resourse-Input.png)
Syntax
Let’s have a look at the syntax.
Open the VBA editor by holding down the ALT key and press F11. On the insert
tab choose Insert module. Type in Sub Test and hit the enter key.
Then type any single word followed by an equal sign. Type InputBox(
The syntax will then be available as shown below. The Microsoft table below
explains each parameter. You will notice that the Prompt is mandatory and is
inserted between double quotes.
You will rarely use [Xpos] (4) and [Ypos] (5) which are the left and top positions
for the Input box. Helpfile (6) and ContextID (7) are seldom used.
Prompt (1), Title (2), Default (3) and Type (8) are the parameters we will use
the most.
(http://www.onlinepclearning.com/wp-content/uploads/2013/10/input-box-
syntax.png)
Input Function
The input box function will allow the user to enter a value into the input box;
however the data is always coerced to text and would return a string value.
Input Method
If we add the word application before the InputBox syntax we turn the Function
into a Method. Option Type (8) now appears. This will allow us to set the data
type for the input box. No other type will be allowed. Below is a list of type
values and their respective meanings.
Type argument
1. If we want a cell reference, then we must use the Set keyword to show
we are referring to the cell object.
So if you do not use the Set statement, the variable is set to the value in the
range, rather than the Range object itself.
That is the theory behind the Input Box. Now we will look at some practical
examples.
Sub InputSimple()
'Input variable
End Sub
VBA Examples
I have added the four examples below to help with the understanding of the
InputBox function and Method. After these four examples I have included four
small practical projects that I think you will find very helpful.
You will also be able to download two workbooks containing three of the
projects.
1. A user login
2. An age calculator
3. The BMI calculator
4. Print area range tool
Creating a user login with the Input box is relatively easy. We can use the Input
box Function in order to accomplish this. In the example below we are adding
the value of the Input box into the active sheet range A1.
Sub InputName()
'Input variable
Range("A1").Value = AddName
End Sub
In this example we are adding a variable that finds the next available row to
into which we will add our data.
Sub Login()
'Copy location
'Input variable
AddData.Value = AddName
End Sub
With this login code we need to add the names of those allowed to login
directly into the VBA procedure. In this instance it is, Trevor, Harry and Mary.
Sub Login2()
'Copy location
'Input variable
Case "Trevor"
AddData.Value = AddName
Case "Harry"
AddData.Value = AddName
Case "Mary"
AddData.Value = AddName
Case Else
End Select
End Sub
Here is a practical example of all of the features we have just mentioned plus
extending the flexibility by allowing the number of users to login to be as many
as we require.
To do this we loop through the range of valid users and compare this with the
value that is typed into the Input box.
If the wrong password is added three times, the workbook will close and not
be saved.
Note:
Be careful if you edit this code because it is possible to create a procedure
that cannot be exited.
Sub LoginCheck()
'Count attempts
'Destination location
'Input variable
Auth = 0
AddData.Value = Nme
Auth = 1
Exit For
End If
Next cName
If Auth = 0 Then
Check.Value = Check.Value + 1
LoginCheck
End If
End Sub
Collect employee age
Example 1 Age list
In this example we are just simply collecting the age of the employee and
adding it to a list on the worksheet. The ages are added one after the other in
column five.
Sub InputAge()
'Input variable
'Copy location
End Sub
You will notice that we do all of the calculations on the worksheet and return
the calculated value to the user in a message box at the end of the procedure.
Note:
The result will appear in the message box. Notice we are adding this cell
values to the message box content.
Sub InputAge()
Sheet1.Range("F8") = AddAge
End If
End Sub
BMI Calculator
Example: Basic BMI Calculator (Input Method)
Two input boxes
are called one
after the other
and the data is
stored on the
worksheet.
In this example
we are using the
Input box Method
by inserting
Application
before the
keyword
InputBox. The
data type that we
have chosen is
(1) indicating that
we are limiting
this to numbers only. We show two input boxes one after the other to collect
the Height and then the Weight which is necessary to calculate our BMI.
A formula is added to cell H8 to calculate the BMI for us. This is stored as a
variable and shown in the message box result.
H8 formula =G8/((F8/100)*(F8/100))
Sub BMI_Calculator()
Sheet2.Range("F8") = Height
Else
Exit Sub
End If
Sheet2.Range("G8") = Weight
Else
Exit Sub
End If
End Sub
Note:
Type (8) is used for the data type. A cell reference as a range object>
Sub InputPrint()
'Declare the variable
'Input variable
ActiveSheet.PageSetup.PrintArea = PrintArea.Address
Application.Dialogs(xlDialogPrint).Show
On Error GoTo 0
End Sub
Conclusion
The Input box function and the Input box Method are awesome tools for
collecting data from a user and then using that in our calculations and
decisions in our applications. It presents us with the opportunity to very allow
the user to interact with the processes we have set.
We have now discussed the four decision modelling tools at our disposal.
1. If function
2. Select Case function
3. Message box function
4. Input box function and method.
With these functions we can help the end user to make effective decisions as
they use our applications.
Related
Comments are closed.