How can I run this macro in multiple sheets simultaneously?

JakeG

New Member
Joined
Apr 13, 2021
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
I have a macro that I'm using to alphabetize an attendance list and then delete the 'Blank' rows (the cells actually return "" so, when sorted in descending order, they appear above the sorted list of names)

Below is the code, that seems to work perfectly - but it currently only runs in the sheet labelled "1", and I would like it to run in 8 sheets which are all labelled in numerical order, i.e. "1", "2", "3", "4", "5", "6", "7" and "8"


VBA Code:
Sub SortPOSurnames()
'
' test
'

    ActiveWorkbook.Worksheets("1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("1").Sort.SortFields.Add Key:=Range("D3:D42"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("1").Sort
        .SetRange Range("A3:E42")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    With ActiveSheet
    .AutoFilterMode = False
    With Range("d1", Range("d" & Rows.Count).End(xlUp))
        .AutoFilter 1, ""
        On Error Resume Next
        .Offset(2).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With



End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,026
Office Version
  1. 365
Platform
  1. Windows
There are probably ways to simplify things a little, but here is a quick way to loop through sheets 1-8 and to run the code against each one:
VBA Code:
Sub SortPOSurnames()
'
' test
    
    Dim s As Long

    For s = 1 To 8
        s.Activate
        ActiveWorkbook.Worksheets(s).Sort.SortFields.Clear
        ActiveWorkbook.Worksheets(s).Sort.SortFields.Add Key:=Range("D3:D42"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets(s).Sort
            .SetRange Range("A3:E42")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        With ActiveSheet
            .AutoFilterMode = False
            With Range("d1", Range("d" & Rows.Count).End(xlUp))
                .AutoFilter 1, ""
                On Error Resume Next
                .Offset(2).SpecialCells(12).EntireRow.Delete
            End With
            .AutoFilterMode = False
        End With

End Sub
 
Solution

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,523
Office Version
  1. 2019
Platform
  1. Windows
Hi, welcome to forum

Untested but see if this update to your code will do what you want

VBA Code:
Sub SortPOSurnames()
    Dim ws          As Worksheet
    Dim i           As Integer
    
    For i = 1 To 8
    
        Set ws = ActiveWorkbook.Worksheets(CStr(i))
        
        With ws
            
            With .Sort.SortFields
                .Clear
                .Add Key:=ws.Range("D3:D42"), SortOn:=xlSortOnValues, _
                                              Order:=xlAscending, _
                                              DataOption:=xlSortNormal
            End With
            
            With .Sort
                .SetRange ws.Range("A3:E42")
                .Header = xlGuess
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            
            .AutoFilterMode = False
            
            On Error Resume Next
            
            With .Range("d1", .Range("d" & .Rows.Count).End(xlUp))
                .AutoFilter 1, ""
                .Offset(2).SpecialCells(12).EntireRow.Delete
            End With
            .AutoFilterMode = False
        End With
        
        Set ws = Nothing
        On Error GoTo 0
    Next i
    
End Sub

Dave
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,523
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Worked perfectly - thank you !

glad found solution that helped you - we appreciate the feedback

Dave
 

JakeG

New Member
Joined
Apr 13, 2021
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
There are probably ways to simplify things a little, but here is a quick way to loop through sheets 1-8 and to run the code against each one:
Apologies - didn't see this post, thanks very much !
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,026
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad we were able to help.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,258
Messages
5,641,162
Members
417,195
Latest member
Vishal kumar

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
Top