Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Trimming Spaces from Strings.

Trimming Spaces from Strings

Written by Allen Wyatt (last updated January 6, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


4

It is often necessary to trim spaces off of strings when programming macros. For instance, let's say you used the InputBox function to get some user input. The function returns a string, but you find out that the user hit the space bar a few times before typing a response. Thus, you end up with a string such as " My String," complete with leading spaces.

Fortunately, VBA provides several different functions to remove spaces from a string. The following are the three functions you could use:

MyVar = LTrim(MyVar)
MyVar = RTrim(MyVar)
MyVar = Trim(MyVar)

The first example ends up trimming all the spaces from the left end of the string, the second removes them from the right end, and the third removes them from both ends. You can use the function that you feel best fits your programming needs.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12593) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Trimming Spaces from Strings.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Can't Select Style Instances

Using the Styles and Formatting task pane, Word allows you to select all instances of a given style in your document. ...

Discover More

Cannot Convert a Document File to a Readable Document

Open a Word document file, and you can start to make edits and changes to your heart's content. But what happens if the ...

Discover More

Synchronizing Lists

Two lists of similar data can be challenging to synchronize. Here are some ways that you can align data in two different ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Checking the Scope of a Defined Name

When creating a macro to work with the names of defined ranges, you may need to know whether the scope of the name is for ...

Discover More

Adding Buttons to Your Worksheet

You can easily add a button to your worksheet that will allow you to run various macros. This tip shows how easy it is.

Discover More

Relative VBA Selections

Need to select a cell using a macro? Need that selection to be relative to the cell you currently have selected? Here are ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 0 + 4?

2022-01-07 06:54:37

Peter Atherton

RKeev

Char 160 is a non-breaking character (hard space) and is not deleted by any Trim functions. Two consectutive hard-spaces will also remain in place wiith trim.
Fig 1 is done in Word and figure 2 is in Excel
[ (see Figure 1 below)

This is fig. 1

Figure 1. 


2022-01-07 06:51:37

Peter Atherton

RKeev

Char 160 is a non-breaking character (hard space) and is not deleted by any Trim functions. Two consectutive hard-spaces will also remain in place wiith trim.
Fig 1 is done in Word and figure 2 is in Excel
[ (see Figure 1 below)

HTH

Figure 1. 


2022-01-06 08:16:44

RKeev

Do any/all of these remove the CHAR 160 from the cell as well?


2022-01-06 06:11:15

Willy Vanhaelen

There is a fourth possibility: Application.TRIM(MyVar).
Excel's TRIM not only removes leading and trailing spaces but it replaces also multiple spaces within the string with only one space.
So Application.TRM("     test     it     "), becomes "test it".


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.