Adjust recorded macro to run for 50 sheets

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
471
Office Version
  1. 365
Platform
  1. Windows
Hi could any adjust this recorded macro so it would run for 50 sheets?
Recorded macro is used to sort data via column
Sheets are named 1,2,3,4,5,6,7 and so on
Or maybe somone has a better way to do this ?
Thanks
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B78").Select
    ActiveWindow.SmallScroll Down:=63
    Range("B78:D157").Select
    ActiveWorkbook.Worksheets("1 ").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("1 ").Sort.SortFields.Add2 Key:=Range("D78:D157" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("1915 ").Sort
        .SetRange Range("B78:D157")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWindow.SmallScroll Down:=-54
    Range("E78").Select
    ActiveWindow.SmallScroll Down:=54
    Range("E78:G157").Select
    ActiveWorkbook.Worksheets("1 ").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("1 ").Sort.SortFields.Add2 Key:=Range("G78:G157" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("1915 ").Sort
        .SetRange Range("E78:G157")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
UNTESTED

VBA Code:
Sub Macro1()
Dim n As Integer
 For n = 1 To 50
    Worksheets(n).Activate
    Range("B78:D157").Select
    ActiveSheet.Sort.SortFields.Clear
  ActiveSheet.Sort.SortFields.Add Key:=Range("D78:D157"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("B78:D157")
        .Header = xlGuess
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("E78:G157").Select
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("G78:G157" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("E78:G157")
        .Header = xlGuess
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Next n
End Sub
 
Upvote 0
Solution
UNTESTED

VBA Code:
Sub Macro1()
Dim n As Integer
For n = 1 To 50
    Worksheets(n).Activate
    Range("B78:D157").Select
    ActiveSheet.Sort.SortFields.Clear
  ActiveSheet.Sort.SortFields.Add Key:=Range("D78:D157"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("B78:D157")
        .Header = xlGuess
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("E78:G157").Select
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("G78:G157" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("E78:G157")
        .Header = xlGuess
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Next n
End Sub
Thanks alot, that works great
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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