简体   繁体   中英

Excel VBA Macro called from VB IDE works fine, but fails when called from a button until file is saved multiple times

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 ( 如此处所见 ), 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 ( 如此处所见 ). 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 this post until the author mentioned he's doing a specific Ribbon Button setup and called the wrong macro - this is not my problem. . .

After much troubleshooting, I think I finally found the cause of this dilemma. It dawned on me that the stupid thing was crashing more and more and more, so, I did what any reasonable person would - copied all the sheets over to a brand new xlsm book. Then, I copied the text of the VBA Modules into a separate Text editor, and recreated them all. All the forms I had were safe (THANKFULLY) so exporting those and importing them to the new one was simple enough to do.

A major pitfall to be aware of: there was a warning about links to other files - turns out it had kept links to the old document in the BUTTONS (of all things)!! I used Edit Links under the Data Ribbon 菜单 (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM