remove trailing spaces from over 300,000 rows

Dundee Lad

Active Member
Joined
Sep 6, 2003
Messages
311
Hello board, I need some help.

I have some simple vba that loops through each cell in column BJ of my worksheet to remove the spaces at the end however this is taking a huge amount of time and I was hoping there was a faster way that someone could show me?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
thanks W8253, but need to use VBA. This also removes all extra spaces from a cell, front, middle and end. only want to remove from
 
Upvote 0
Select the column containing the data,

Ctrl+H to find and replace

Find What = hit the space bar once

Replace with = leave blank
 
Last edited:
Upvote 0
I don't know how to write VBA.

type some samples, then i can write you a formula
 
Last edited:
Upvote 0
Hi, you could see if either of these are any faster:

Code:
Sub TrimCells1()
With Range("BJ1:BJ" & Range("BJ" & Rows.Count).End(xlUp).Row)
    .Cells = Evaluate(Replace("=IF(RIGHT(@,1)="" "",LEFT(@,LEN(@)-1),@)", "@", .Address))
End With
End Sub

Code:
Sub TrimCells2()
Dim v, i As Long
v = Range("BJ1:BJ" & Range("BJ" & Rows.Count).End(xlUp).Row).Value
For i = 1 To UBound(v)
    If Right(v(i, 1), 1) = " " Then v(i, 1) = Left(v(i, 1), Len(v(i, 1)) - 1)
Next i
Range("BJ1").Resize(UBound(v)).Value = v
End Sub
 
Upvote 0
You can try this

Code:
Sub test2()
Dim LR As Long, i As Long
LR = Range("BJ" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("BJ" & i)
      [COLOR=#B22222][B]  If Len(.Value) Then [/B][/COLOR].Value = Trim(Right(.Value, Len(.Value) - 1))
    End With
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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