VBA Trim, LTrim, and RTrim Functions – Remove Spaces From Text

This tutorial will demonstrate how to use the Trim, LTrim, and RTrim VBA functions as well as the Trim worksheet function.

Trim Function

The VBA Trim function removes (“trims”) erroneous spaces before and after strings of text.

Trim Spaces Before and After Text

The VBA Trim function will remove spaces before and after strings of text:

Sub TrimExample_1()
MsgBox Trim(" I love excel ")		
'Result is: "I love excel"

MsgBox Trim(" I love excel")		
'Result is: "I love excel"

MsgBox Trim("I love excel ")		
'Result is: "I love excel"
End Sub

Trim Multiple Spaces Before and After Text

This includes trimming multiple spaces before and after text:

Sub TrimExample_2()
MsgBox Trim("     I love excel          ")		
'Result is: "I love excel"

MsgBox Trim("      I love excel")			
'Result is: "I love excel"

MsgBox Trim("I love excel             ")		
'Result is: "I love excel"
End Sub

VBA Trim will NOT Remove Multiple Spaces Between Words

However, the Trim function will not remove multiple spaces in between words:

Sub TrimExample_3()
MsgBox Trim("     I love    excel          ")		
'Result is: "I love    excel"

MsgBox Trim("      I  love excel")			
'Result is: "I  love excel"

MsgBox Trim("I love        excel             ")		
'Result is: "I love        excel"
End Sub

Trim as a Worksheet Function

However, the Excel Trim worksheet function can be used to remove extra spaces between words:

trim worksheet remove extra spaces

 

 

 

 

 

Use Worksheet Trim Function in VBA

To use the Excel Trim Function in VBA, call it by using WorksheetFunction:

Sub TrimExample_4()
Msgbox WorksheetFunction.Trim("     I love    excel          ")	
'Result is: "I love excel"

Msgbox WorksheetFunction.Trim("      I  love excel")		
'Result is: "I love excel"

Msgbox WorksheetFunction.Trim("I love        excel             ")	
'Result is: "I love excel"
End Sub

Difference Between WorksheetFunction.Trim and VBA Trim

This will demonstrate the differences between Trim and WorksheetFunction.Trim:

Sub TrimExample_5()
Msgbox WorksheetFunction.Trim("     I love    excel          ")	
'Result is: "I love excel"
Msgbox Trim("     I love    excel          ")				
'Result is: "I love    excel"

Msgbox WorksheetFunction.Trim("      I  love excel")		
'Result is: "I love excel"
Msgbox Trim("      I  love excel")					
'Result is: "I  love excel"

Msgbox WorksheetFunction.Trim("I love        excel             ")	
'Result is: "I love excel"
Msgbox Trim("I love        excel             ")				
'Result is: "I love        excel"

End Sub

Use VBA to add Trim Function in a Range

The Trim Worksheet function can be added in a Range using property .Formula:

Sub TrimExample_6()
ThisWorkbook.Worksheets("Sheet1").Range("B1").Formula = "=trim(A1)"		
End Sub

LTrim Function

The LTrim function removes spaces only from the left side of the word:

Sub TrimExample_7()
MsgBox LTrim(" I love excel ")			
'Result is: "I love excel "

MsgBox LTrim(" I love excel")			
'Result is: "I love excel"

MsgBox LTrim("I love excel ")			
'Result is: "I love excel "

MsgBox LTrim("   I love   excel   ")		
'Result is: "I love   excel   "

MsgBox LTrim("   I   love excel")			
'Result is: "I   love excel"

MsgBox LTrim("I love    excel   ")			
'Result is: "I love    excel    "
End Sub

RTrim Function

The RTrim function removes spaces only from the right side of the word:

Sub TrimExample_8()
MsgBox RTrim(" I love excel ")			
'Result is: " I love excel"

MsgBox RTrim(" I love excel")			
'Result is: " I love excel"

MsgBox RTrim("I love excel ")			
'Result is: "I love excel"

MsgBox RTrim("   I love   excel   ")		
'Result is: "   I love   excel"

MsgBox RTrim("    I    love excel")		
'Result is: "    I    love excel"

MsgBox RTrim("I    love excel    ")		
'Result is: "I     love excel    "
End Sub

Trim, Ltrim and Rtrim do not remove spaces between words.

Remove all spaces from text

Trim will only remove extra spaces in between words, but to remove all spaces in a string of text, you can use the Replace Function:

Sub ReplaceExample ()
MsgBox Replace("     I love     excel ", " ", "")		
'Result is: "Iloveexcel"
End Sub