0% found this document useful (0 votes)
121 views

How To Use The SPACE Function (VBA) : Macro Code

This document explains how to use the Excel SPACE function in VBA. The SPACE function returns a string with a specified number of spaces. It has one required number parameter. Examples show calling SPACE(3) returns " " and SPACE(7) returns " ". The function can be used to add spaces between strings in VBA code like concatenating "Hello" & SPACE(10) & "World".

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

How To Use The SPACE Function (VBA) : Macro Code

This document explains how to use the Excel SPACE function in VBA. The SPACE function returns a string with a specified number of spaces. It has one required number parameter. Examples show calling SPACE(3) returns " " and SPACE(7) returns " ". The function can be used to add spaces between strings in VBA code like concatenating "Hello" & SPACE(10) & "World".

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MS Excel: 

How to use the SPACE Function (VBA)

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

 VBA function (VBA)

Example (as VBA Function)


The SPACE function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel SPACE function examples and explore how to use the SPACE function in
Excel VBA code:

Space(3)
Result: "   "

Space(7)
Result: "       "

For example:

Dim LResult As String

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.

The syntax of the function is:

Space( Number )
Where the Number argument is the number of spaces making up the returned String.

VBA Space Function Examples

' Example 1 - Create a String containing 5 spaces.


Dim str1 As String
str1 = Space( 5 )
' The variable str1 is now equal to "     " (five spaces).
' Example 2 - Create a String containing 0 spaces.
Dim str2 As String
str2 = Space( 0 )
' The variable str2 is now equal to "" (an empty
string).

In the above example:

 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).

VBA Space Function Errors


If you supply a negative Number argument to the VBA Space function, you will get the error:
Run-time error '5': Invalid procedure call or argument
If the Number that is supplied to the Space function is a text string that cannot be converted
into a numeric value, you will get the error:
Run-time error '13': Type mismatch

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

You might also like