VBA TRIM Column

Jeffreyxx01

Board Regular
Joined
Oct 23, 2017
Messages
156
Hi guys,

can someone help to fix my macro?
Thanks,

Code:
Sub Trim_column()
 
Dim LastRow As Long
Dim Sht as Worksheet
 
Set Sht = ActiveWorkbook.Sheets("DataBase")
 
With Sht
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("A2:A" & LastRow).value = Trim(Range(“A2:A” & LastRow).Value)
      
End With
 
End Sub
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about
Code:
Sub MyTrim()
With Range("A1", Range("A" & Rows.Count).End(xlUp))
   .Value = Evaluate("if({1},trim(" & .Address & "))")
End With
End Sub
 
Upvote 0
Are all the columns the length?
 
Upvote 0
Yes, but are all those columns the same length?
 
Last edited:
Upvote 0
Is the last used row in each column the same?
 
Upvote 0
How about
Code:
Sub MyTrim()
   Dim lr As Long
   lr = Cells.find("*", , xlValues, , xlByRows, xlPrevious, , , False).Row
   With Range("A1:H" & lr)
      .Value = Evaluate("if({1},trim(" & .Address & "))")
   End With
End Sub
This will trim col B as well if that's not a problem
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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