Remove leading and trailing spaces keeping multiple spaces between words intact

MikeJ100

New Member
Joined
Jan 30, 2017
Messages
41
Office Version
  1. 365
Platform
  1. Windows
How can I remove leading and trailing spaces keeping multiple spaces between words intact? I notice the TRIM function removes multiple spaces between words .
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Check this -

All Records.xlsb
CD
3 Sanjay Gulati Musafir Sanjay Gulati Musafir
Sheet2
Cell Formulas
RangeFormula
D3D3=SUBSTITUTE(SUBSTITUTE(C3," ","",LEN(C3)-LEN(SUBSTITUTE(C3," ","")))," ","",1)
 
Upvote 0
Check this -
I don't know what is possible with the OP's data but your formula seems to only allow for a single leading space (not more, not less) and a single trailing space (not more, not less). If that were the case, this would do the same job
=MID(C3,2,LEN(C3)-2)

@MikeJ100
As suggested above, please update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

If you have all the latest functions then you could try this formula solution for any number of leading/trailing spaces, including none.

22 10 10.xlsm
ABCDE
1For info, trailing spaces in col ATo show no trailing spaces in col B
2Big DogBig Dog0Dog
3 Big Dog Big Dog2Dog
4 Big Black Dog Big Black Dog4Dog
5Big Dog Big Dog1Dog
Trim Left & Right Only
Cell Formulas
RangeFormula
B2:B5B2=LET(s, SEQUENCE(LEN(A2)),c,MID(A2,s,1),ts,TEXTSPLIT(TEXTJOIN("|",1,IF(c=" ","",s)),"|")+0,MID(A2,MIN(ts),MAX(ts)-MIN(ts)+1))
E2:E5E2=RIGHT(B2,3)
 
Upvote 0
Check this -

All Records.xlsb
CD
3 Sanjay Gulati Musafir Sanjay Gulati Musafir
Sheet2
Cell Formulas
RangeFormula
D3D3=SUBSTITUTE(SUBSTITUTE(C3," ","",LEN(C3)-LEN(SUBSTITUTE(C3," ","")))," ","",1)
Hi Sanjay, thanks for your post.

When I use the formula you suggested I get

1665393299888.png
 
Upvote 0
Possibly an easier way to go, especially if you do not have the functions used in my post above, is to employ a very simple user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function LRTRIM(s As String) As String
  LRTRIM = RTrim(LTrim(s))
End Function

MikeJ100.xlsm
AB
1
2Big DogBig Dog
3 Big Dog Big Dog
4 Big Black Dog Big Black Dog
5Big Dog Big Dog
Trim Left & Right Only (2)
Cell Formulas
RangeFormula
B2:B5B2=LRTRIM(A2)
 
Upvote 0
I have updated my account details. I am using 365 and Windows. Sadly, Peter's suggestion doesn't work.
 
Upvote 0
Sadly, Peter's suggestion doesn't work.
That gives us nothing to go on.;)

Does it crash Excel?
Does it produce an error value? If so what?
Does it do nothing?
Does it remove all spaces?
Something else?

Thanks for updating your version though. (y)
 
Upvote 0
Thanks for your help, however when I use Peter's suggestion for a macro and his formula I get

1665394723119.png
 
Upvote 0
I have updated my account details. I am using 365 and Windows. Sadly, Peter's suggestion doesn't work.
If you are using the formulae I gave or @Peter_SSs gave there are negligible chances of getting error. But still if you are telling, it means there is some problem somewhere.

If you may post the actual formulae you have posted to identify where the challenge could be...
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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