Macro to trim trailing Spaces in Col A & B

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have written code to remove/trim the trailing spaces in Col A & B on sheet 2

Eg BR1 Shirt Sales

in this eg remove space/s only after sales


The macro trims the data in Col A , but not in Col B




It would be appreciated if someone could kindly assist me




Code:
 Sub TrimtrailingSpaces()
 Sheets(2).Select
 
FirstRow = 1 
LastRow = Cells(Rows.Count, 1).Resize(, 2).End(xlUp).Row
For Each cell In Range(Cells(FirstRow, 1), Cells(LastRow, 1))
cell.Value = Trim(cell.Value)
Next cell

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Your range is only looking in column 1:

Code:
Range(Cells(FirstRow, 1), Cells(LastRow, [COLOR=#ff0000]1[/COLOR]))

so change the red 1 to 2 ie column 1 to column 2.

This will be faster if you want to use it:

Code:
Dim x As Variant, lr As Long

lr = Application.Max(Range("A" & Rows.Count).End(xlUp).Row, Range("B" & Rows.Count).End(xlUp).Row)
 
Set x = Range("A1:B" & lr)
x = Evaluate("=IF(ROW(1:" & lr & "),TRIM(" & x.Address & "))")
Range("A1:B" & lr) = x
 
Upvote 0
If you want it to do both columns A and B, you need to update the column reference in your range reference to do column B (or "2"), i.e.
Code:
For Each cell In Range(Cells(FirstRow, 1), Cells(LastRow, [COLOR=#ff0000][B]2[/B][/COLOR]))
or
Code:
For Each cell In Range(Cells(FirstRow, [COLOR=#ff0000][B]"A"[/B][/COLOR]), Cells(LastRow, [COLOR=#ff0000][B]"B"[/B][/COLOR]))
 
Upvote 0
Your range is only looking in column 1:

Code:
Range(Cells(FirstRow, 1), Cells(LastRow, [COLOR=#ff0000]1[/COLOR]))

so change the red 1 to 2 ie column 1 to column 2.

This will be faster if you want to use it:

Code:
Dim x As Variant, lr As Long

lr = Application.Max(Range("A" & Rows.Count).End(xlUp).Row, Range("B" & Rows.Count).End(xlUp).Row)
 
Set x = Range("A1:B" & lr)
x = Evaluate("=IF(ROW(1:" & lr & "),TRIM(" & x.Address & "))")
Range("A1:B" & lr) = x
The TRIM function will reduce multiple consecutive internal spaces to single spaces and will delete leading spaces. If these are possibilities for the OP's data and if the OP truly wants to delete only the trailing spaces (which his thread title suggests), then he would need something along these lines instead...
Code:
[table="width: 500"]
[tr]
	[td]Sub TrimTrailingSpaces()
  Dim Addr As String
  Addr = "A1:B" & Columns("A:B").Find("*", , xlValues, , xlRows, xlPrevious).Row
  Range(Addr) = Evaluate(Replace("IF(@="""","""",LEFT(@,FIND(""|"",SUBSTITUTE(@,RIGHT(TRIM(@)),""|"",LEN(@)-LEN(SUBSTITUTE(@,RIGHT(TRIM(@)),""""))))))", "@", Addr))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Thanks Rick. That's exactly what I require
 
Upvote 0
If you want to delete trailing spaces you can use RTrim function.
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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