Need Excel Formula - Removing trailing spaces that doesn't use clean or trim

wadezone

New Member
Joined
Apr 23, 2018
Messages
7
I am doing some data comparison/testing and need a way to remove trailing spaces from the end of each cell. The number of spaces at the end could vary depending on the data. Clean/TRIM is removing spaces in the middle that I must retain. I have also trying using SUBSTITUTE to replace but again is removing data in the middle of the text. Any ideas?

Thanks in advance
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Welcome to the forum.

Is VBA acceptable?
 
Upvote 0
CSE formula (Ctrl-Shift-Enter)
=MID(A1,1,MAX((MID(A1,ROW(INDIRECT("1:"& LEN(A1))),1)<>" ")*ROW(INDIRECT("1:"& LEN(A1)))))
 
Upvote 0
I prefer where I could use the excel formula and maybe append it to some other string manipulation I already have in my template. I then compare the source and target using exact. What do you have in VBA? I figured you are going to loop through the string starting from the right until I get an alphanumeric value. I get the impression that this may not be possible by excel formula. I thought there was a LTRIM or RTRIM but no luck.
 
Upvote 0
VBA does have RTrim, so it would simply be something like:

Code:
Function RTRIM(sIn as String) as String
RTRIM = VBA.RTrim$(sIn)
End Function

You can use that in a formula just like you would use TRIM, as long as you don't need to pass multiple-cell ranges or arrays to it.
 
Last edited:
Upvote 0
Good point Rory, thanks for the reminder. I am curious what INDIRECT will do to see if it works even though the VBA function is cleaner. I will let you both know how it goes in a bit. Thank you again.

 
Upvote 0
Just bear in mind that INDIRECT is volatile so any formula that uses it (or depends on a cell that does) will recalculate whenever the workbook does, even if its input cells haven't actually changed. If you use it a lot, you will probably notice a slowdown.
 
Upvote 0
You can get rid of the volatile INDIRECT function (which would be recommended if you have lots of data) by using the following CSE formula instead. Replace 99 with whatever will bound the max number of characters in your data items.

=MID(A1,1,MAX((MID(A1,ROW($1:$99),1)<>" ")*(MID(A1,ROW($1:$99),1)<>"")*ROW($1:$99)))
 
Last edited:
Upvote 0
Here is a normally entered, non-volatile formula for you to consider...

=LEFT(A1,FIND("|",SUBSTITUTE(A1,RIGHT(TRIM(A1)),"|",LEN(A1)-LEN(SUBSTITUTE(A1,RIGHT(TRIM(A1)),"")))))

And, if you don't mind array-entered** formulas, here are two more non-volatile formulas that should work...

=LEFT(A1,MAX((MID(A1&REPT(" ",99),ROW($1:$99),1)<>" ")*ROW($1:$99)))

=LEFT(A1,MAX((MID(A1,ROW($1:$99),1)=RIGHT(TRIM(A1),1))*ROW($1:$99)))

**Commit the last two formulas using CTRL+SHIFT+ENTER and not just Enter by itself
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,155
Messages
6,123,331
Members
449,098
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top