## Overview of VBA Trim Function:

VBA Trim function is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It removes leading, trailing and In-between spaces from a given string. It has one mandatory String parameter. If input string has Null, then it returns null. It could be used only in VBA function. We can’t use this function in Excel worksheet. The Trim function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA Trim function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Trim function, where we can use this Trim function and real-time examples in VBA.

**Table of Contents:**

- Overview of VBA Trim Function
- Syntax of VBA Trim Function
- Parameters or Arguments
- Where we can apply or use the VBA Trim Function?
- Example 1: Remove extra spaces from a given string:” Awesome “
- Example 2: Remove extra spaces from a given string:” Life is Beautiful “
- Example 3: Remove extra spaces from a given string with spaces:”123 456 789″
- Example 4: Remove spaces from a given empty string:” “
- Instructions to use Macro Codes

### Syntax of VBA Trim Function

The syntax of the VBA Trim function is

Trim (String)

Note: This Trim function removes leading, trailing and In-between spaces from a given string.

### Parameters or Arguments

This function has one mandatory parameter or argument for the Trim Function.

Where

**string:** The string is a mandatory argument. The string which we want to convert to Upper case letters.

### Where we can apply or use the VBA Trim Function?

We can use this VBA Trim function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

### Example 1: Remove extra spaces from a given string:” Awesome “

Here is a simple example of the VBA Trim function. This below example macro removes leading and trailing spaces from a given string.

'Remove extra spaces from a given string:" Awesome " Sub VBA_Trim_Function_Ex1() Dim sString As String, sSubString As String sString = " Awesome " sSubString = Trim(sString) MsgBox "(" & sSubString & ")", vbInformation, "VBA Trim Function" End Sub

**Output**: Here is the screen shot of the first example output.

### Example 2: Remove extra spaces from a given string:” Life is Beautiful “

Here is a simple example of the VBA Trim function. This below example macro removes all leading, trailing and In-between (extra) spaces from a given string.

'Remove extra spaces from a given string:" Life is Beautiful " Sub VBA_Trim_Function_Ex2() Dim sString As String, sSubString As String sString = " Life is Beautiful " sSubString = Trim(sString) MsgBox "(" & sSubString & ")", vbInformation, "VBA Trim Function" End Sub

**Output**: Here is the screen shot of the second example output.

### Example 3: Remove extra spaces from a given string with spaces:”123 456 789″

Here is a simple example of the VBA Trim function. This below example macro removes in-between spaces from a given string.

'Remove extra spaces from a given string with spaces:"123 456 789" Sub VBA_Trim_Function_Ex3() Dim sString As String, sSubString As String sString = "123 456 789" sSubString = Trim(sString) MsgBox "(" & sSubString & ")", vbInformation, "VBA Trim Function" End Sub

**Output**: Here is the screen shot of the third example output.

### Example 4: Remove spaces from a given empty string:” “

Here is a simple example of the VBA Trim function. This below example macro removes all spaces and returns an empty string.

'Remove spaces from a given empty string:" " Sub VBA_Trim_Function_Ex4() Dim sString As String, sSubString As String sString = " " sSubString = Trim(sString) MsgBox "(" & sSubString & ")", vbInformation, "VBA Trim Function" End Sub

**Output**: Here is the screen shot of the fourth example output.

### Instructions to use Macro Codes

Here are the step by step instructions to use the VBA Trim function in Excel VBA.

- Open an Excel workbook.
- Press Alt+F11 to open VBA Editor window.
- Go to Insert menu and click on module from the available options.
- Copy above specified macros to the VBA editor.
- Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

#### Complete List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

List of VBA Functions