The VBA Space Function

Related Function:
VBA String


The VBA Space function creates a String consisting of a specified number of spaces.

The syntax of the function is:

Space( Number )

Where the Number argument is the number of spaces making up the returned String.

VBA Space Function Examples

' Example 1 - Create a String containing 5 spaces.
Dim str1 As String
str1 = Space( 5 )
' The variable str1 is now equal to "     " (five spaces).
' Example 2 - Create a String containing 0 spaces.
Dim str2 As String
str2 = Space( 0 )
' The variable str2 is now equal to "" (an empty string).

In the above example:

VBA Space Function Errors

If you supply a negative Number argument to the VBA Space function, you will get the error:

Run-time error '5': Invalid procedure call or argument

VBA Run Time Error 5 Message Box

If the Number that is supplied to the Space function is a text string that cannot be converted into a numeric value, you will get the error:

Run-time error '13': Type mismatch

VBA Run Time Error 13 Message Box