A little VBA/Macro code adjustment

Excel_User_10k

Board Regular
Joined
Jun 25, 2022
Messages
100
Office Version
  1. 2021
Platform
  1. Windows
Hi, here is an easy one for you...but still beyond me unfortunately.

I have recorded a macro to sort a sheet of dynamic data and I have one for each month.

It does what I need it to but the recorded code is specific to the worksheet I recorded it on. I know I don't need to duplicate this code for every single monthly worksheet and there should just be an "active.worksheet" code in there somewhere instead. But I do not know how to make this adjustment after several failed attempts.

I would like to just be able to assign the same code to every button created for each monthly worksheet and it only run on the sheet that it is clicked.

Thank You.

This is the code as it stands:

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

'
    ActiveWorkbook.Worksheets("April").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("April").Sort.SortFields.Add2 Key:=Range("P13:P45") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("April").Sort.SortFields.Add2 Key:=Range("E13:E45") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("April").Sort
        .SetRange Range("B13:S45")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub


Exactly the same scenario with this code too please.

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

'
    ActiveWorkbook.Worksheets("April").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("April").Sort.SortFields.Add2 Key:=Range("P2:P6"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("April").Sort.SortFields.Add2 Key:=Range("E2:E6") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("April").Sort
        .SetRange Range("D2:S6")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("O9").Select
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,223,944
Messages
6,175,554
Members
452,652
Latest member
eduedu

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