Macro to remove leading spaces in Col A

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I would like to trim leading spaces to the left in Col A on sheets BR1_sales to BR12_Sales


Code:
 Sub Remove_Leading_Spaces_CommSheets()
      Dim I As Long, LR As Long
      
    For I = Worksheets("BR1_Sales").Index To Worksheets("BR12_sales").Index
        With Worksheets(I)
         LR = .Cells(Rows.Count, "A").End(xlUp).Row
      For Each r In ActiveSheet.Range("A1:A" & LR)
 If Left(r.Value, 1) = " " Then r.Value = Right(r.Value, Len(r.Value) - 1)
 
 Next I
 Next r
 End With
 
 End Sub


However, when running the macro , I get a compile error: "invalid next control variable reference"


It would be appreciated if someone could kindly amend my code
 

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"
I haven't checked anything else but for one thing your Next I and Next r are in the wrong order. Swap them.
 
Upvote 0
Actually, there are quite a few things wrong and/or not ideal in that code.

For one thing, if you take more care with your indentations, you would more easily see where things do not match

1585972126695.png



VBA Code:
Sub Remove_Leading_Spaces_CommSheets()
  Dim I As Long, LR As Long
  Dim r As Range
  
  For I = Worksheets("BR1_Sales").Index To Worksheets("BR12_sales").Index
    With Worksheets(I)
      LR = .Cells(Rows.Count, "A").End(xlUp).Row
      For Each r In .Range("A1:A" & LR)
        r.Value = LTrim(r.Value)
      Next r
    End With
  Next I
End Sub
 
Upvote 0
Thanks Peter

I swopped them around and when I run the macro, I now get error message next without for

Kindly check and advise
 
Upvote 0
Ingore my last reply. Never saw your second reply
 
Upvote 0
Thanks Peter. Your code works perfectly
 
Upvote 0
Thanks Peter. Your code works perfectly
You're welcome.

To emphasise my earlier point, here is your original code with coloured lines that should not cross.

1585972665926.png
 
Upvote 0
thanks for your input and pointing out where I went wrong
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,036
Members
449,205
Latest member
Eggy66

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