Run the same Macro on All WorkSheets

edmore

New Member
Joined
Feb 20, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello

I would like to run this code on all worksheets. Currently, it works well on one sheet.

How do I change the code?

Many Thanks

VBA Code:
Sub Sort_by_colour()
'
' Sort_by_colour Macro
'

'
    Columns("A:A").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("A1:A116"), _
        xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(192, 0 _
        , 0)
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A116")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Give this a try,
VBA Code:
Sub Sort_by_colour()
'
' Sort_by_colour Macro
'

'
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add(Range("A1:A116"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(192, 0 _
            , 0)
        .SetRange Range("A1:A116")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 1
Solution
Give this a try,
VBA Code:
Sub Sort_by_colour()
'
' Sort_by_colour Macro
'

'
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add(Range("A1:A116"), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(192, 0 _
            , 0)
        .SetRange Range("A1:A116")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
How do I run on selected Sheets such as Sheet1, Sheet3, Sheet5, etc...
 
Upvote 0
You could run it on a given sheet by first going to that sheet, which would make it the ActiveSheet, then run the macro.

Alternatively, if you always want it to be run on a given set of sheets, you could change it to:
VBA Code:
Sub Sort_by_colour()
'
' Sort_by_colour Macro
'

'
    Dim varList As Variant
    Dim varWks As Variant
    Dim wksToUse As Worksheet
    '
    varList = Array("Sheet1", "Sheet3", "Sheet5")
    For Each varWks In varList
        Set wksToUse = ThisWorkbook.Worksheets(CStr(varWks))
        With wksToUse.Sort
            .SortFields.Clear
            .SortFields.Add(wksToUse.Range("A1:A116"), _
                xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(192, 0 _
                , 0)
            .SetRange wksToUse.Range("A1:A116")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next varWks
End Sub
Next question: Is the range always going to be A1:A116, or do you need that generalised as well?
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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