Macro Loop through cells in column

jakobt

Active Member
Joined
May 31, 2010
Messages
337
Have a column C with a lot of data. Many of the cells contain spaces in the end. I want to trim each cell by using a macro looping through each cell.
I want to practice looping.
Is it possible to write a code which loops through each cell and removes the space(s) in each cell.

Thanks.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
jakobt,

You might consider the following...

Code:
Sub anotherLoop()
Dim r As Range
For Each r In Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
    r.Value = Trim(r.Value)
Next r
End Sub

Cheers,

tonyyy
 
Upvote 0
jakobt,

Here is another macro solution for you to consider that does not do any looping thru the cells in column C.

Please test the macro in a copy of your workbook.

Code:
Sub RemoveSpaceCharacterFromEndOfCell()
' hiker95, 07/16/2018, ME1063114
Dim Addr As String
Addr = "C2:C" & Cells(Rows.Count, "C").End(xlUp).Row
Range(Addr) = Evaluate(Replace("IF(Right(@,1)="" "",LEFT(@,LEN(@)-1),@)", "@", Addr))
End Sub
 
Last edited:
Upvote 0
jakobt,

The macro in my last reply will only remove the last space character in the string, and, NOT all trailing space characters.

I will be back later today after checking my archives.

I hope that Rick Rothstein will respond to this thread with one of his magical Range(Addr) = Evaluate(Replace( solutions.
 
Upvote 0
See if this does what you want.
Code:
Sub RemoveSpaces()
  With Range("C2", Range("C" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",trim(#))", "#", .Address))
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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