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 a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Dundee Lad

Active Member
Joined
Sep 6, 2003
Messages
311
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
 

vicedo

Active Member
Joined
Jan 9, 2015
Messages
401
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:

Dundee Lad

Active Member
Joined
Sep 6, 2003
Messages
311

ADVERTISEMENT

Thanks Vicedo, but this also removes all spaces and is not VBA.
 

vicedo

Active Member
Joined
Jan 9, 2015
Messages
401
I don't know how to write VBA.

type some samples, then i can write you a formula
 
Last edited:

Dundee Lad

Active Member
Joined
Sep 6, 2003
Messages
311

ADVERTISEMENT

Thanks vicedo, I know the formula, BUT, I need to use VBA.
 

FormR

MrExcel MVP
Joined
Aug 18, 2011
Messages
6,502
Office Version
  1. 365
Platform
  1. Windows
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
 

vicedo

Active Member
Joined
Jan 9, 2015
Messages
401
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
 

Watch MrExcel Video

Forum statistics

Threads
1,130,444
Messages
5,642,168
Members
417,258
Latest member
amk1979

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