Macros Not Working Every Time - Excel VBA


I have a simple workbook with two macros and everytime I open the workbook I have to assign the macro to the button. Why doesn't it just stay assigned? Please help. Workbook attached.

Both macros are on the Overview tab and are very simple macros. One just deletes sheets and the other creates sheets.

Thank you-
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In order to save the keyboard shortcut you need to select the correct location in the dropdown. I recommend you use the actual file in the Macro options:macro optionsI have uploaded the file where I have assigned the shortcuts as Ctrl-W and Ctrl-E
Roy CoxGroup Finance ManagerCommented:
Your code will error if it tries to delete or hide all the sheets in the workbook

Does this amended code work as you want. Note, it refers to ThisWorkBook so will only work in the workbook containing the code and not delete sheets in other workbooks by accident, also Select case is tidier in this case. I have also removed unused variables

Option Explicit

Sub mcr_delete_tabs()
' mcr_delete_tabs Macro
    Dim ws As Worksheet

    Application.DisplayAlerts = False
    For Each ws In ThisWorkbook.Worksheets
        Select Case ws.Name
        Case "Overview", "By_Matter", "Sheet1"
        Case Else
            ws.Visible = xlSheetVisible
        End Select
    Next ws
    Application.DisplayAlerts = True
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.