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
547 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 ...
If there's likely to be a common response to the question you're asking, you can save your users time by providing a default value for the input box:
A default value is displayed in the input box as soon as it appears. You can accept the default value, or simply type in a new one.
You don't need any files for this part of the lesson.
You can click here to download a file containing the finished code.
Create a new workbook and insert a new module.
You can use the optional Default parameter to provide an initial value for an input box. Create a subroutine called DefaultCake and add code to it as shown below:
This code assigns the string Chocolate to the default value of the input box.
Running the code shown above produces an input box with a value already entered:
You could type over the default value but there seems little point in this particular case.
You don't have to pass a literal value to the default parameter; you can also use the result of an expression. Create a new subroutine called DefaultDate and add code to it as shown below:
This code uses the Date and Format functions to pre-fill the input box with a formatted version of the current date.
Running this procedure displays an input box with today's date entered as the default:
The default value you see will depend on the date when you run the procedure.
To practise setting a default value for an input box:
Sub DefaultDogs()
Dim PetChoice As String
PetChoice = InputBox( _
Prompt:="Dogs or cats?", _
Title:="Best Pets")
End Sub
Sub DefaultDogs()
Dim PetChoice As String
PetChoice = InputBox( _
Prompt:="Dogs or cats?", _
Title:="Best Pets", _
Default:="Dogs")
End Sub
Sub DefaultDogs()
Dim PetChoice As String
PetChoice = InputBox( _
Prompt:="Dogs or cats?", _
Title:="Best Pets", _
Default:="Dogs")
If LCase(PetChoice) = "dogs" Then
MsgBox "Correct! Doggos are the best."
ElseIf LCase(PetChoice) = "cats" Then
MsgBox "Wrong! Cats are the worst."
Else
MsgBox "Wrong, " & PetChoice & _
" are rubbish, dogs are the best." & _
vbNewLine & "Or maybe sloths."
End If
End Sub
Remember: dogs are best, but anything is better than a cat. Even spiders.
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.