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



## Saher Naji (Dec 15, 2022)

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



```
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
```


----------



## Micron (Dec 15, 2022)

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.


----------



## Saher Naji (Dec 15, 2022)

Micron said:


> 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!


----------



## Saher Naji (Dec 17, 2022)

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)


----------

