Trim Function

nbob

Board Regular
Joined
Nov 9, 2011
Messages
52
I have Column J2 and K2. J2 contains first names in my database; K2 Last Names. Unfortunately when the data is sent to me all the names have about 5-9 extra spaces which need to go. Currently I can select a cell and use =trim(k2) drag this down the list of 18000+ rows and then copy>past special>values in the original column but i know there has got to be an easier way. I know the following does not work...at all.

Code:
Dim LResult As String
LResult = Trim("k2:k19000")
Any ideas?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try

Code:
Sub TrimRange()
Dim C As Range
 
For Each C In Range("K2:K19000")
    C.Value = Trim(C.Value)
Next C
End Sub
 
Upvote 0
How's this:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> LR <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>        LR = Cells(Rows.Count, "K").End(xlUp).Row<br>        <br>        <SPAN style="color:#00007F">With</SPAN> Range("L1:L" & LR)<br>            .FormulaR1C1 = "=Trim(RC[-1])"<br>                 <SPAN style="color:#00007F">With</SPAN> Range("K1:K" & LR)<br>                    .Value = Range("L1:L" & LR).Value<br>                    .Value = .Value<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            .Value = ""<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

I'm sure there's a better way, but it's been a long day, and my brain hurts...
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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