VBA SPACE Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

The VBA SPACE function is listed under the text category of VBA functions. When you use it in a VBA code, it returns a string which is consisting of a specified number of spaces. In simple words, with the space function, you can create spaces by supplying numbers.

Syntax

Space(Number)

Arguments

  • Number: A number to specify the number of spaces for making up the String in the result.

Example

To practically understand how to use the VBA SPACE function, you need to go through the below example where we have written a vba code by using it:

example-vba-space-function
Sub example_SPACE()
Range("A1").Value = Space(5)    
End Sub

In the above code, we have used SPACE to create a string in cell B1 with the 5 spaces as we have specified 5 in the argument.

Notes

  • If the number supplied is a negative number then VBA will return a run-time 5 error.