Keep VBA code working if I run it from a button in another sheet

Saher Naji

Board Regular
Joined
Dec 19, 2019
Messages
76
Office Version
  1. 2013
Platform
  1. Windows
Hello,
I'm trying to sort the date in an excel sheet, I want to click on a button to do this, but button is not at the same sheet
I assigned the macro in a button in the same sheet, it worked very well, but when I moved the button to another sheet it's not working!
what should I edit in this code?

I also attached a screen-shot, maybe there is a formula instead of the this VBA code, or make the VBA code run without click on any buttons
I don't have Office 365, so =FILTER functions is not working

Thanks a lot


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

'
    Sheets("Jan_List").Select
    Range("K2:R4").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.End(xlUp).Select
    Range("T2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Jan_List").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Jan_List").Sort.SortFields.Add2 Key:=Range( _
        "T2:T1241"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Jan_List").Sort
        .SetRange Range("T2:AA1241")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

1671123416155.png
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
So you want to run this code against Jan_List sheet data only, but from any sheet? Then move the code to a standard module. That way, its scope is the whole workbook. You can run it from keyboard shortcut that you create, or perhaps some sheet event (but you'd need code in that event that calls the moved sub, on every sheet that you want to run it from). As for a formula, not my thing so I will leave that to others to make suggestions.
 
Upvote 0
So you want to run this code against Jan_List sheet data only, but from any sheet? Then move the code to a standard module. That way, its scope is the whole workbook. You can run it from keyboard shortcut that you create, or perhaps some sheet event (but you'd need code in that event that calls the moved sub, on every sheet that you want to run it from). As for a formula, not my thing so I will leave that to others to make suggestions.
Thanks @Micron
In fact I prefer a formula, but it's not easy, I'm okay with a VBA code, run without click on any buttons
I did not know how to move the code to a standard module!
 
Upvote 0
Now I know what is the problem, but I can't fix it
The button is linked, and at the same time assigned to macro, when I removed the link it worked, but I need to keep it linked.
Is it not allowed to assigned a macro to a linked button? (I mean button have a clickable link to move me to a specific sheet)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,560
Members
449,089
Latest member
Motoracer88

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