How to use the VBA SPACE Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA SPACE Function (Syntax + Example)

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 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 VBA SPACE function, you need to go through the below example where we have written a vba code by using it:

Sub example_SPACE()
Range("A1").Value = Space(5)    
End Sub

In the above code, we have used the SPACE to create a string in the 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.

Related Functions

FORMAT | INSTR | INSTRREV | LCASE | LEFT | LEN | LTRIM | MID | REPLACE | RIGHT | RTRIM | STRCOMP | STRCONV | STRING | STRREVERSE | TRIM | UCASE