Search
Close this search box.

VBA – Space String


The Space() function in VBA is used to generate a string consisting of a specified number of spaces.

Syntax:

Space(number)

Parameter:

 

  • number: The required number of spaces to be generated. This argument must be an integer between 0 and 32767.

Return Value:

  • String: Returns a string containing the specified number of spaces.

 

Example

				
					Dim str1 As String
Dim str2 As String

str1 = Space(5) ' str1 will be "     " (five spaces)
str2 = Space(0) ' str2 will be an empty string ("")
				
			

Use Cases:

  • Formatting Output: Adding spaces to align text or create visual spacing in reports or forms.
  • String Manipulation: Creating strings with a specific number of leading or trailing spaces.
  • Data Padding: Padding data with spaces to ensure consistent field lengths.