VBA Trim Function – How to cut spaces from left and right side of the string

1. Trim VBA Function – Description

The Trim VBA function removes spaces from both sides of the text. We use it when we want to remove unnecessary spaces from both the left and right side of a text string. It is worth noting that the function does not remove spaces from the middle of the text, only from the left and right sides. If we want to remove spaces from the middle of the text, we should use the VBA Replace function . If, on the other hand, we would like to remove spaces only from the left or only from the right side of the text, we should use the LTrim function to remove spaces from the left side or RTrim to remove unnecessary spaces from the right side of the text.

2. VBA Trim Function – Syntax

Trim (String)

String – The text string from which we want to remove unnecessary spaces from the left and right sides.

Function return: String / text value.

3. VBA Trim function – Example

How to use the Trim  function  in VBA Excel? Below is an example of using the Trim  function  in the VisualBasic Editor.

Sub TrimExample ()

MsgBox Trim ("Free text with spaces on both sides")
Range ("A1") = Trim ("Any text with spaces on both sides")

End Sub
Excel VBA functions - Trim VBA function
Excel VBA functions – Trim VBA function

4. Trim VBA function – Where to use?

The function can be used in: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016.

The article is part of the VBA Excel function list. You can find a list of all VBA functions at this address:  VBA functions.

Leave a comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: