Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Up to this point we've been using the generic VBA input box to ask users for input. Microsoft Excel has its own version of an input box with several additional features which make it more useful than the standard version:
The Excel input box doesn't look much different to the generic VBA version, but it has a few hidden tricks which make it much more useful!
You don't need any files for this section.
Click here to download the sample file used on this page.
Create a new workbook and insert a new module in the VBE.
The Excel-specific InputBox function is a member of the Application object. To display this type of input box, you must start by referring to the Application object. Create a new subroutine called ExcelInputBox and add code to it as shown below:
To access the Excel input box, write Application.InputBox in your code.
The Excel InputBox function has eight parameters in total, but only the Prompt parameter is required:
This code is sufficient to make an Excel input box appear.
Running the code shown above produces an input box as shown in the diagram below:
So far, so simple.
If you're going to be using a mix of VBA and Excel input boxes in your code, you might find it useful to always be explicit about which type you're using:
'show a generic VBA input box
VBA.InputBox "Enter your name"
'show an Excel-specific input box
Application.InputBox "Enter your name"
You can capture the result of an Excel input box in the same way as for the generic VBA version. Add code to declare a String variable and return the result of the input box to the variable:
By default the input box returns a string, so you can capture it in a String variable. Don't forget to enclose the argument list in parentheses (round brackets) when you want to capture the return value of the input box.
You can customise the appearance of an Excel input box by changing its title and providing a default value. Add code to display a title and default value on the input box:
Here we've used named arguments.
Running the code shown above produces an input box similar to the one shown below:
Sadly, this is all you can do to alter the appearance of an input box.
To practise using an Excel input box:
Sub WhatsTheDifference()
Application.InputBox "Enter your name"
End Sub
This is the type of input box you should see.
Sub WhatsTheDifference()
Dim YourName As String
Application.InputBox "Enter your name"
End Sub
Sub WhatsTheDifference()
Dim YourName As String
YourName = Application.InputBox("Enter your name")
End Sub
Sub WhatsTheDifference()
Dim YourName As String
YourName = Application.InputBox("Enter your name")
MsgBox "Hello " & YourName
End Sub
Feel free to embellish the message box with a title and icon.
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.