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 ...
One of the interesting things about the Excel input box is that it is modeless. This means that you can click on cells in the Excel window while the input box is displayed on screen:
Clicking on a cell will read its address into the input box.
Click here to download the file needed for this part of the lesson.
You can click here to download a workbook containing the completed code.
Download and extract the workbook linked to in the Files Needed section above.
You can use an Excel input box to ask the user to select a cell and capture its contents in a variable. Insert a new module and create a subroutine called ReturnCellContents. Add code to return the result of an Excel input box to a String variable as shown below:
Here we're using a String variable and have not specified the Type parameter of the input box.
When you execute the code show above, you can click on a cell while the input box is displayed:
Click on a cell to select it and read its address into the input box. Click OK to return the cell's contents to the variable.
After clicking OK on the input box, a message box will appear which shows the value of the selected cell:
The message box shows the value captured in the variable.
You can select multiple cells when the input box is displayed:
Here we've selected a block of six cells.
The results of this selection may not be quite what you expect:
The variable only captures the value of the top left cell in the selection.
You can also select non-adjacent cells by holding Ctrl while clicking on them:
Hold Ctrl and click to select non-adjacent cells.
You won't be able to proceed with such a selection, however:
This is the message you'll see if you select non-adjacent cells.
You can set the Type parameter of the Excel input box to 1, which means that it will only accept numeric values. Create a new subroutine called SelectANumber. Add code to return the result of an Excel input box to a Double variable as shown below:
Set the Type parameter to 1 to make sure the input box returns a number.
After executing the code shown above, you can select a cell containing a number:
Select any cell containing a number.
After clicking OK on the input box, a message box appears and shows the value that was selected:
This basic message box shows that the value of the cell you selected was captured in the variable.
You can try to select a cell which doesn't contain a number:
This cell clearly doesn't contain a number.
You'll see the standard warning message whenever you enter a non-numeric value into an input box with a Type of 1:
Maybe try reading the instructions next time?
To practise using an input box to select cells:
Sub MoviePicker()
Dim BestFilm As String
BestFilm = Application.InputBox( _
Prompt:="Pick the best film")
End Sub
Sub MoviePicker()
Dim BestFilm As String
Dim WorstFilm As String
BestFilm = Application.InputBox( _
Prompt:="Pick the best film")
WorstFilm = Application.InputBox( _
Prompt:="Pick the worst film")
End Sub
There's only really one choice for worst film.
Sub MoviePicker()
Dim BestFilm As String
Dim WorstFilm As String
BestFilm = Application.InputBox( _
Prompt:="Pick the best film")
WorstFilm = Application.InputBox( _
Prompt:="Pick the worst film")
wsMovies.Range("B16").Value = BestFilm
wsMovies.Range("B17").Value = WorstFilm
End Sub
Your selected films should appear as shown here.
Sub MoviePicker()
Dim BestFilm As String
Dim WorstFilm As String
BestFilm = Application.InputBox( _
Prompt:="Pick the best film")
If BestFilm = "False" Then Exit Sub
WorstFilm = Application.InputBox( _
Prompt:="Pick the worst film")
If WorstFilm = "False" Then Exit Sub
wsMovies.Range("B16").Value = BestFilm
wsMovies.Range("B17").Value = WorstFilm
End Sub
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.