How To Use The SPACE Function (VBA) : Macro Code
How To Use The SPACE Function (VBA) : Macro Code
This Excel tutorial explains how to use the Excel SPACE function with syntax and examples.
Description
The Microsoft Excel SPACE function returns a string with a specified number of spaces.
The SPACE function is a built-in function in Excel that is categorized as a String/Text Function. It can
be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro
code that is entered through the Microsoft Visual Basic Editor.
Syntax
The syntax for the SPACE function in Microsoft Excel is:
Space( number )
Parameters or Arguments
number
The number of spaces to be returned.
Returns
The SPACE function returns a string value.
Applies To
Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010,
Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Function
Space(3)
Result: " "
Space(7)
Result: " "
For example:
LResult = Space(5)
In this example, the variable called LResult would now contain the value " ".
Description
The VBA Space function creates a String consisting of a specified number of spaces.
Space( Number )
Where the Number argument is the number of spaces making up the returned String.
The first call to the VBA Space function returns the String " " (five spaces);
The second call to the VBA Space function returns the String "" (zero spaces - an
empty string).
Sub AddSpaces()
Dim MyString As String
MyString = "Hello" & Space(10) & "World"
MsgBox MyString
End Sub
Sub AddSpaces()
Dim MyString As String
MyString = "Hello" & " " & "World"
MsgBox MyString
End Sub