VBA SPACE Function

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

What is VBA SPACE Function

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.

how to use it

To use VBA's SPACE function you need to understand its syntax and arguments:

Syntax

Space(Number)

Arguments

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

Example to use SPACE Function in VBA

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

Below are some important points which you need to take care while using SPACE function in VBA.

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

Related Functions

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.