VBA SPACE Function

VBA SPACE Function in Excel

VBA Space function in Excel is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns a string with a specified number of spaces. VBA Space function has one mandatory number parameter. If input value is Null or negative value, then it returns an error.

We use only as a VBA function. We can not use this function in Excel. The Space function can be used in either procedure or function in a VBA editor window in Excel. Use this VBA Space function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Space function, where we can use this Space function and real-time examples in VBA.

Syntax of VBA Space Function

The syntax of the VBA Space function is

Space(Number)

Note: This Space function returns a string with a specified number of spaces.

Parameters or Arguments

This function has one mandatory parameter or argument for the Space Function.
Where
Number:The number is a mandatory argument. It represents a number of spaces to be returned as a string.

Where we can apply or use the VBA Space Function?

We can use this VBA Space function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Create a string with 5 spaces

Here is a simple example of the VBA Space function. This below example macro returns a string with 5 spaces.

'Create a string with 5 spaces
Sub VBA_Space_Function_Ex1()

    Dim iInput As Integer, sOutput As String
    
    iInput = 5
    
    sOutput = Space(iInput)
    
   MsgBox "String with 5 Spaces: <" & sOutput & ">", vbInformation, "VBA Space Function"

End Sub

Output: Here is the screen shot of the first example output.
VBA Space Function

string with no space

Here is a simple example of the VBA Space function. This below example macro returns empty string.

'Create a string with no space
Sub VBA_Space_Function_Ex2()

    Dim iInput As Integer, sOutput As String
    
    iInput =0
    
    sOutput = Space(iInput)
    
    MsgBox "String with 0 Spaces: <" & sOutput & ">", vbInformation, "VBA Space Function"

End Sub

Output: Here is the screen shot of the second example output.
VBA Space Function

If space function input number is negative

Here is a simple example of the VBA Space function. This below example returns an error.

'If space function input number is negative
Sub VBA_Space_Function_Ex3()

    Dim iInput As Integer, sOutput As String
    
    iInput =-5
    
    sOutput = Space(iInput)
    
    MsgBox "<" & sOutput & ">", vbInformation, "VBA Space Function"

End Sub

Output: Here is the screen shot of the third example output.
VBA Space Function

Space function input number is null

Here is a simple example of the VBA Space function. This below example macro returns an error.

'If space function input number is null
Sub VBA_Space_Function_Ex4()

    Dim iInput As Integer, sOutput As String
    
    iInput = Null
    
    sOutput = Space(iInput)
    
    MsgBox "<" & sOutput & ">", vbInformation, "VBA Space Function"

End Sub

Output: Here is the screen shot of the fourth example output.
VBA Space Function

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply