Written by Allen Wyatt (last updated January 6, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
When writing a macro, you may want to fill a range of cells with different values. The easiest way to do this is to use ...
Discover MoreNeed to click on a cell and have it replaced with an "X"? Macros make it easy to do, as illustrated in this tip.
Discover MorePaste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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".
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments