Search
Close this search box.

VBA – LTrim String


The LTrim function in VBA is used to remove all leading spaces (spaces at the beginning) from a string

Syntax:

LTrim(string)

Parameter:

 

  • string: The string from which you want to remove leading spaces.


Return Value:

  • String: Returns the same string without any leading spaces.

 

Example

				
					Dim str1 As String
Dim trimmedStr As String

str1 = "   Hello, World!" 
trimmedStr = LTrim(str1) 

' trimmedStr will be "Hello, World!"
				
			

Use Cases:

  • Data Cleaning:
    • Removing extra spaces from user input.
    • Preparing data for database entry or processing.
  • String Comparisons:
    • Ensuring accurate string comparisons by removing any leading spaces that might affect the result.
  • Data Formatting:
    • Removing unnecessary spaces from the beginning of text fields in reports or forms.

Key Considerations:

  • LTrim only removes leading spaces. It does not affect trailing spaces or spaces within the string.
  • For removing both leading and trailing spaces, use the Trim function.