Home > Net >  Excel VBA Macro called from VB IDE works fine, but fails when called from a button until file is sav
Excel VBA Macro called from VB IDE works fine, but fails when called from a button until file is sav

Time:12-28

I've seen a few posts addressing things similar to what I'm experiencing, but nothing definitively resolves my issue. If I need to post this in a different Stack forum, please let me know.

I have multiple Buttons on an Excel sheet (NOT ActiveX CommandButtons) which are displaying some strange behaviors.

They are all assigned various Macros, but they all fail within certain conditions, except one (see "TLDR" below). I get the standard "Cannot run the macro ''Workbook Name Here.xlsm'!'Subroutine_Called''. The macro may not be available in this workbook or all macros may be disabled." error. I get this error on ALL of the macros assigned to the 15 buttons.

Each Macro indeed exists. When executing the Macros from the VB IDE they work just fine. When executing the Macros from the Macros button ("View ToolBar" --> "Macros") and selecting the Macros and clicking the Run button, they work just fine. When looping through the buttons through VBA to pull the Button.OnAction string and pulling out the workbook name, and then calling THAT from within the Loop works just fine. The Code is not the problem. It's a problem with Excel itself.

Here's the kicker (TLDR): I've already selected the "Enable Macros" button when the security warning first pops up when the workbook opens (as seen here), but the Macros fail to run from the buttons. If I save it a few times and open it again, there will be a Macro security warning at the top, just under the formula bar (as seen here). The Macros still do not work from the Buttons even then (Again - they all work fine from VB IDE or the Run Macro dialog from the View toolbar). The only time the Macros work from the Buttons is when the document is a Trusted Document. How do I change this behavior, and can it be done programmatically?

My Concern is it exposes the names of the Macros and any/all parameters being sent in plain text which I am hoping to avoid. Has anyone else experienced this specific phenomena? I thought I had an answer in menu (grayed out now because they're all gone! WOOT!

Everything seems to be working fine now!

I have to give this StackOverflow article (and subsequently allula at Ozgrid as well even though the linked article there is broken) some major Kudos. This is what led me to this solution to begin with. I'll be cross commenting on the linked post about my experience too in case nobody finds this one.

Additionally, as an added protection measure, I made the path of the workbook a trusted path and provided a subroutine to give the intended users this option without having to do it manually, only doing so when they accept it and removing it if they move the file and select "no" on the same request prompt.

  • Related