564 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls 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 ...
Passing arguments to subroutines in VBA Part three of a three-part series of blogs |
---|
To become an efficient programmer, you need to stop writing one long subroutine and start writing lots of small ones instead - and to do this, you're going to need to know everything there is to know about passing arguments.
You can find more articles on this subject in our Excel Visual Basic tutorial blog - or book onto one of our Excel or VBA training courses. |
In our BuildSnowman routine at the start of this blog, sometimes we wanted buttons and a bow tie, and sometimes we didn't. The way to get round this is to make arguments optional.
Suppose we want to be able to construct an address (perhaps for use in a mail merge, although this isn't shown here), and we don't know how many lines it will have. Here are 2 possible addresses we might have:
![]() |
![]() |
One address line only | A full postal address |
A routine which would display an address whether it has one line, two lines or two lines and a postcode is shown below:
Sub CreateAddress( _
PersonName As String, _
AddressLine1 As String, _
Optional AddressLine2 As String = "", _
Optional PostCode As String = "")
'create the full address for mail merge
Dim FullAddress As String
'start with first line of address
FullAddress = PersonName & vbCrLf & AddressLine1
'if there is a second line, add it on with line break
If Len(AddressLine2) > 0 Then _
FullAddress = FullAddress & vbCrLf & AddressLine2
'if there is a postcode, add this on with line break
If Len(PostCode) > 0 Then _
FullAddress = FullAddress & vbCrLf & PostCode
'display result
MsgBox FullAddress
End Sub
By making the last two arguments optional you can miss them out (in which case they'll take the default value shown). The syntax of an optional argument is thus:
Optional ArgumentName as ArgumentType = DefaultValue
Here's how you could call the CreateAddress above with different argument combinations:
Sub TestAddresses()
'3 different combinations of arguments (the message box for the first
'and the last are shown above)
CreateAddress "Bob the Builder", "Sunflower Valley"
CreateAddress "Tinky-Winky", "1 The Avenue", "Teletubbieland"
CreateAddress "William Pitt", "10 Downing Street", "London", "SW1"
End Sub
The main rule of optional arguments is that they should come at the end of the list.:
This code shows an error because the optional arguments aren't at the end of the list
If you think hard about it, you'll see why: otherwise, how would VBA know which value to assign to which argument?
In our address example above, we could have addresses with anything from 0 to 6 lines. Rather than having to list 6 optional arguments, we could create an optional array, called a ParamArray. The easiest way to explain this is by example:
Sub CreateAddress( _
PersonName As String, _
ParamArray AddressLines() As Variant)
'create the full address for mail merge
Dim FullAddress As String
'start with first line of address
FullAddress = PersonName
'add on any other lines
Dim AddressLine As Variant
For Each AddressLine In AddressLines
FullAddress = FullAddress & vbCrLf & AddressLine
Next AddressLine
'display result
MsgBox FullAddress
End Sub
You could then call this CreateAddress routine with any number of arguments you liked (including none at all):
Sub TestAddresses()
'3 different combinations of arguments
CreateAddress "Wise Owl"
CreateAddress "Bob the Builder", "Sunflower Valley"
CreateAddress "Tinky-Winky", "1 The Avenue", "Teletubbieland"
CreateAddress "William Pitt", "10 Downing Street", "London", "SW1"
End Sub
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
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 2023. All Rights Reserved.