Can someone help me loop this macro to each tab?

andreafeliz

New Member
Joined
Jul 13, 2016
Messages
6
I want to run this macro for each tab. It sorts the same range A5:H48 on each tab. It is sorted by date range oldest to newest in column A.

Help! Thank you.

Sub LoopTabSort()
'
' LoopTabSort Macro
' Sorts same range A5:H48 on each tab. Cell sorted by date range oldest to newest in column A
'
' Keyboard Shortcut: Ctrl+j
'
ActiveCell.Range("A1:H44").Select
ActiveWorkbook.Worksheets("Adams #1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Adams #1").Sort.SortFields.Add Key:=ActiveCell, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Adams #1").Sort
.SetRange ActiveCell.Range("A1:H44")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWindow.SmallScroll Down:=-42
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Untested but try....

Code:
Sub LoopTabSort()
    '
    ' LoopTabSort Macro
    ' Sorts same range A5:H48 on each tab. Cell sorted by date range oldest to newest in column A
    '
    ' Keyboard Shortcut: Ctrl+j
    '

    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Sort.SortFields.Clear
        ws.Sort.SortFields.Add Key:=ws.Cells(1, "A"), _
                               SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                               xlSortTextAsNumbers
        With ws.Sort
            .SetRange ws.Range("A1:H44")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next

End Sub
 
Upvote 0
Does this work?

Code:
[COLOR=#333333]Sub LoopTabSort()[/COLOR]
[COLOR=#333333]'[/COLOR]
[COLOR=#333333]' LoopTabSort Macro[/COLOR]
[COLOR=#333333]' Sorts same range A5:H48 on each tab. Cell sorted by date range oldest to newest in column A[/COLOR]
[COLOR=#333333]'[/COLOR]
[COLOR=#333333]' Keyboard Shortcut: Ctrl+j[/COLOR]
[COLOR=#333333]'

[/COLOR]Dim S as Worksheet

For each S in ActiveWorkbook.Worksheets

[COLOR=#333333]    S.Sort.SortFields.Clear[/COLOR]
[COLOR=#333333]    S.Sort.SortFields.Add Key:=S.Range("A1"), _[/COLOR]
[COLOR=#333333]    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _[/COLOR]
[COLOR=#333333]    xlSortTextAsNumbers[/COLOR]
[COLOR=#333333]    With S.Sort[/COLOR]
[COLOR=#333333]        .SetRange S.Range("A1:H44")[/COLOR]
[COLOR=#333333]        .Header = xlNo[/COLOR]
[COLOR=#333333]        .MatchCase = False[/COLOR]
[COLOR=#333333]        .Orientation = xlTopToBottom[/COLOR]
[COLOR=#333333]        .SortMethod = xlPinYin[/COLOR]
[COLOR=#333333]        .Apply[/COLOR]
[COLOR=#333333]    End With[/COLOR]
[COLOR=#333333]    ActiveWindow.SmallScroll Down:=-42

[/COLOR]Next S

[COLOR=#333333]End Sub

[/COLOR]
 
Last edited:
Upvote 0
Similarly:
Code:
Sub LoopTabSort()
 '
 ' LoopTabSort Macro
 ' Sorts same range A5:H48 on each tab. Cell sorted by date range oldest to newest in column A
 '
 ' Keyboard Shortcut: Ctrl+j
 '
Dim xlWkSht As Worksheet
For Each xlWkSht In ActiveWorkbook.Worksheets
  With xlWkSht.Sort
    .SortFields.Clear
    .SortFields.Add Key:=ActiveCell, SortOn:=xlSortOnValues, _
      Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    .SetRange ActiveCell.Range("A5:H48")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
Next
End Sub
Note: You refer to "range A5:H48 on each tab" but the code you posted refers to A1:H44. You need to sort that out (no pun intended) - I've opted for A5:H48.
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,875
Members
449,192
Latest member
MoonDancer

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