REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

The VBA Space function is used to generate a string of spaces, with the specified number of spaces as the argument. This function is useful for formatting data and creating space between text or numbers in a string. It is commonly used in VBA programming to manipulate strings and present data in a more organized manner.

VBA Space Function – Purpose, Syntax and Arguments

Syntax

Space(Number)

Arguments

  • Number: Required. It is a numeric value that specifies the number of spaces to be generated in the string. It can be any integer from 0 to 32767.

Example

Suppose we have a VBA code that prompts the user to input their name and we want to display their name with a greeting message, but with equal spacing between the first and last name. We can use the Space function to achieve this as shown in the following code:

Dim firstName as String
Dim lastName as String
firstName = InputBox("Enter your first name:")
lastName = InputBox("Enter your last name:")
Msgbox("Hello" & Space(5) & firstName & Space(5) & lastName)

The code will display the greeting message as follows:

Hello     John     Doe

Remarks and Important Notes

  • The Space function only generates spaces, and any other characters specified within the parentheses will result in an error.
  • If a negative number is specified as the argument, the function will generate an empty string.
  • If the specified number is greater than 32767, the function will return an error as the maximum limit of spaces in a string is 32767.
  • The Space function is not case-sensitive, which means it will always return uppercase spaces.
  • When used in combination with concatenation or other string functions, the Space function can be a powerful tool in VBA programming for manipulating strings and formatting data.

Understanding VBA Space Function with Examples

Example 1: Basic Usage of the SPACE Function

Description: The SPACE function is used to generate a string by repeating the specified number of spaces.

Dim str As String
str = Space(5)
MsgBox str

Explanation: In this example, the SPACE function is used to generate a string of 5 spaces. The variable “str” is assigned with the result of the SPACE function, which is a string of 5 spaces. This string is then displayed using the MsgBox function. The output will be 5 blank spaces without any visible characters.

Example 2: Combining SPACE Function with Other String Functions

Description: The SPACE function can be combined with other string functions to manipulate and format strings.

Dim firstName As String
Dim lastName As String
firstName = "John"
lastName = "Smith"
Dim fullName As String
fullName = firstName & Space(1) & lastName
MsgBox fullName

Explanation: In this example, the SPACE function is used to insert a space between the first and last name. The “&” operator is used to concatenate the first name, a single space (generated by SPACE function), and the last name to create the full name. The output will be “John Smith” with a space in between the names.

Example 3: Using SPACE Function in Loops

Description: The SPACE function can be used in loops to generate a specific number of spaces for formatting purposes.

Dim i As Integer
Dim num As Integer
num = 5
For i = 1 To num
    Debug.Print Space(i)
Next i

Explanation: In this example, the SPACE function is used in a “For” loop to generate a number of spaces equal to the current value of the “i” variable. The loop runs 5 times, where the value of “i” starts at 1 and increments by 1 in each iteration. The output of this code will be 5 lines, with the first line containing 1 space, second line containing 2 spaces, and so on until the fifth line containing 5 spaces.

Example 4: Formatting Strings with SPACE Function

Description: The SPACE function can be used to format strings in a specific way.

Dim str As String
Dim formattedStr As String
str = "Hello"
formattedStr = str & Space(10) & "World"
MsgBox formattedStr

Explanation: In this example, the SPACE function is used to align the word “World” after the word “Hello” with a specific number of spaces in between. The output of this code will be “Hello World” with 10 spaces in between. This can be useful for formatting reports or generating neat output messages.

Example 5: Using SPACE Function to Generate Indentation

Description: The SPACE function can be used to generate indentation in strings.

Dim str As String
Dim indentedStr As String
str = "This is a paragraph."
indentedStr = Space(5) & str & vbCrLf & Space(10) & str & vbCrLf & Space(15) & str
MsgBox indentedStr

Explanation: In this example, the SPACE function is used to generate indentation for multiple lines of text. The variable “indentedStr” is assigned with the result of the SPACE function, followed by the original string, and then the “vbCrLf” function for a new line. The number of spaces is increased in each line to create a neat indentation effect. The output of this code will be a paragraph with 5 spaces at the beginning of the first line, 10 spaces at the beginning of the second line, and 15 spaces at the beginning of the third line.

Conclusion

In conclusion, the SPACE function in VBA can be useful in generating strings with a specific number of spaces, formatting strings, creating indentation, and more. It is a simple yet powerful function that can be used in various scenarios. Understanding the usage and implementation of the SPACE function can help improve the quality and efficiency of VBA coding.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA FunctionsTags: , , , , Last Updated: September 30, 2023

Leave A Comment