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.

  1. Passing Arguments
  2. Passing Arguments by Reference and Value
  3. Optional Arguments and ParamArrays (this blog)

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.

Posted by Andy Brown on 09 February 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Optional Arguments and ParamArrays

Optional Arguments

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:

Single address line A full mailing address
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

Optional Argument Syntax

The main rule of optional arguments is that they should come at the end of the list.:

Example of optional argument error

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?

Using ParamArray

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

The main thing to notice about using ParamArray is that both the array and the variable defined to loop over it must be dimensioned with type Variant.

  1. Passing Arguments
  2. Passing Arguments by Reference and Value
  3. Optional Arguments and ParamArrays (this blog)
This blog has 0 threads Add post