Need help in running the code in different sheets. Thanks

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
This is my current code:
Code:
Sub Left_format()
Dim i As Integer
For i = 1 To 4
With Sheets(i)
    With .Cells
        .HorizontalAlignment = xlLeft
 
    End With
End With
Next i
End Sub
I want this code to run in different 3 sheets, namely; "data", "Update","Tracer"
I have another sheets and I do not want the macro to run there.
THanks alot!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try

Code:
Sub Left_format()
Dim ws As Worksheet
For Each ws In Sheets(Array("data", "Update", "Tracer"))
    With ws
        With .Cells
            .HorizontalAlignment = xlLeft
        End With
    End With
Next ws
End Sub
 
Upvote 0
When i try to run in different code it is giving error please check.

Code:
Sub Left_format()
Dim ws As Worksheet
For Each ws In Sheets(Array("sheet1", "sheet2", "sheet3"))
    With ws
        Range("A2").Select
        Selection.CurrentRegion.Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D2:D" & Selection.Rows.Count + 1) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
       .SetRange Range("A2:F" & Selection.Rows.Count + 1)
          .Header = xlGuess
          .MatchCase = False
          .Orientation = xlTopToBottom
          .SortMethod = xlPinYin
          .Apply
          
    End With
Next ws
End Sub
 
Upvote 0
:warning: This is untested :warning:

Code:
Sub Left_format()
Dim ws As Worksheet, x As Long
For Each ws In Sheets(Array("sheet1", "sheet2", "sheet3"))
    With ws
        With .Range("A2").CurrentRegion
            x = .Rows.Count + 1
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=Range("D2:D" & x) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With .Sort
            .SetRange Range("A2:F" & x)
              .Header = xlGuess
              .MatchCase = False
              .Orientation = xlTopToBottom
              .SortMethod = xlPinYin
              .Apply
            End With
        End With
    End With
Next ws
End Sub
 
Upvote 0
Still giving me error messageruntime error 1004
unable to get sort property of range class
 
Upvote 0
Now tested:

Code:
Sub Left_format()
Dim ws As Worksheet, x As Long
For Each ws In Sheets(Array("sheet1", "sheet2", "sheet3"))
    With ws
        With .Range("A2").CurrentRegion
            x = .Rows.Count + 1
            ws.Sort.SortFields.Clear
            ws.Sort.SortFields.Add Key:=Range("D2:D" & x) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With ws.Sort
            .SetRange ws.Range("A2:F" & x)
              .Header = xlGuess
              .MatchCase = False
              .Orientation = xlTopToBottom
              .SortMethod = xlPinYin
              .Apply
            End With
        End With
    End With
Next ws
End Sub
 
Upvote 0
You are welcome.

I find the XL 2007 code for sorting to be rather kludgy. Try recording a sort using Excel 2003 and below - the code is a lot cleaner (and still works in XL 2007+).
 
Upvote 0

Forum statistics

Threads
1,222,902
Messages
6,168,938
Members
452,227
Latest member
sam1121

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