简体   繁体   English

在 VBA 锁定的 xlsm 文件中调用宏

[英]Calling a Macro in a VBA-locked xlsm file

Im currently trying to write a "controller" File with a macro, that opens other files and just calls 3 Macros in each file.我目前正在尝试编写一个带有宏的“控制器”文件,它可以打开其他文件并在每个文件中调用 3 个宏。 All these files have the same structure.所有这些文件都具有相同的结构。 My Problem is that I can't see the VBA structure, but I do know the Macronames as they are shown to me.我的问题是我看不到 VBA 结构,但我确实知道显示给我的宏名。 But everytime I try to call these macros im getting the following Error Message:但每次我尝试调用这些宏时,我都会收到以下错误消息:

Runtimeerror 1004 The Makro.xxxx can't run.运行时错误 1004 Makro.xxxx 无法运行。 It's maybe not available in this file or all macros have been deactivated.它可能在此文件中不可用,或者所有宏都已停用。

Macros are activated.宏被激活。 If I click the button in that File the macro that gets triggered works perfectly fine.如果我单击该文件中的按钮,则触发的宏可以正常工作。

Any suggestions welcome欢迎任何建议

Thanks谢谢

I've tried everything with wrong filenames with false characters and single commatas.我已经用错误的文件名、错误的字符和单个逗号尝试了一切。 The Opening of the File via Set wb = Workboos.open works perfectly fine, so the file name probably can't be a part of the problem.通过 Set wb = Workboos.open 打开文件非常好,因此文件名可能不是问题的一部分。

You need to specify the name of the workbook you are calling the sub from.您需要指定您从中调用子程序的工作簿的名称。

Suppose you have these subs in both workbooks 1 & 2:假设您在工作簿 1 和 2 中都有这些潜艇:

Public Sub macro1()
    MsgBox "macro1 launched from " & ThisWorkbook.Name
End Sub

Public Sub macro2()
    MsgBox "macro2 launched from " & ThisWorkbook.Name
End Sub

Now you can call the following sub from your controller macro:现在您可以从controller宏中调用以下子程序:

Sub launcher()
    Dim wb As Workbook
    
    Set wb = Workbooks.Open("C:\Users\<username>\Desktop\Workbook1.xlsm")
    
    Application.Run wb.Name & "!" & "macro1" 'the distant macro which you know the name
End Sub

Works perfectly完美运行

Answer to the riddle: Even tho I thought that I checked the File Name with 'Filename.xlsm', the problem resolved when I removed all dashes from the Filenames!谜语答案:尽管我认为我用“Filename.xlsm”检查了文件名,但当我从文件名中删除所有破折号时,问题就解决了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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