VBA Space Function – Add Spaces to a String

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on December 5, 2022

Add Spaces to a String

A common way to add spaces to a string with VBA is to use blank spaces between quotation marks. The following example adds ten spaces between the word “Hello” and “World”:

Sub AddSpaces()
Dim MyString As String

MyString = "Hello" & "          " & "World"

MsgBox MyString

End Sub

VBA Space Function

A more intuitive way to add spaces to a string is to use the Space() function. The following does the same as the previous code, however it uses the Space() function:

Sub AddSpaces()
Dim MyString As String

MyString = "Hello" & Space(10) & "World"

MsgBox MyString

End Sub

Sidenote: This works in VBA, however it doesn’t work in a spreadsheet formula. Instead use the REPT Function to repeat a character, such as a space.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

 

Learn More!

 

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples