VBA Space function

Space Function Description

The VBA Space function returns a string with the required number of whitespaces (” “).

Syntax

The syntax for the Space function in VBA is:

Space( no_of_spaces )

Parameters

no_of_spaces
The number of whitespaces to be returned.

Other Notes

The VBA Space function replicates the following equivalent VBA Code:

Function SpaceX(num As Long)
  Dim i As Long, ret As String
  For i = 1 To num
    ret = ret & " "
  Next i
  SpaceX = ret
End Function

Example usage

The Space function can be used in VBA code. Let’s look at some VBA Space function examples:

Space 4
'Result: "    "

Space 1
'Result: " "