VBA SPACE Function

VBA SPACE Function

Overview of VBA Space Function:

VBA Space function 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. It has one mandatory number parameter. If input value is Null or negative value, then it returns an error. It could be used 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. We can 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.

Table of Contents:

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.

Example 1: 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

Example 2: Create a 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

Example 3: 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

Example 4: If 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 use Macro Codes

Here are the step by step instructions to use the VBA Space function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

Complete List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *