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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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
 
Upvote 0
Solution
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
 
Upvote 0
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 !
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,292
Members
448,885
Latest member
LokiSonic

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