Trim Function

nbob

Board Regular
Joined
Nov 9, 2011
Messages
50
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?
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Try

Code:
Sub TrimRange()
Dim C As Range
 
For Each C In Range("K2:K19000")
    C.Value = Trim(C.Value)
Next C
End Sub
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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...
 

Watch MrExcel Video

Forum statistics

Threads
1,123,259
Messages
5,600,575
Members
414,390
Latest member
plimbu

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
Top