简体   繁体   English

从另一个工作簿运行相同的宏

[英]Run an identical macro from another workbook

First let me explain why I am asking this question so its clear that I need more than the Application.run command. 首先,让我解释为什么我要问这个问题,这样很清楚,我需要的不仅仅是Application.run命令。 If someone has a way solving the problem by other means it would also be good. 如果有人可以用其他方法解决问题,那也很好。

I created a program that will make a new excel file with macro's and codes that is a new program on it's own. 我创建了一个程序,该程序将使用宏和代码创建一个新的excel文件,而该文件本身就是一个新程序。 I did this because I don't want the original file to change. 我这样做是因为我不想更改原始文件。 The original file is the template for all new created files. 原始文件是所有新创建文件的模板。

But when I run my code I get a lot of errors with sheet names that are not in the original file but that are in the new file. 但是,当我运行代码时,工作表名称出现了很多错误,这些错误不在原始文件中,而在新文件中。 So, in some way the Activeworkbook changes and I don't know why. 因此, Activeworkbook在某种程度上发生了变化,我不知道为什么。 When I run the code I changed the Activeworkbook to the new file. 运行代码时,我将Activeworkbook更改为新文件。

I was hoping to solve this by running the macro's from the newly file and making sure these macro's will only work with the newly created workbook and not with the original ones. 我希望通过从新文件运行宏来解决这些问题,并确保这些宏仅适用于新创建的工作簿,而不适用于原始工作簿。 The problem is that the macro's in the original workbook and new workbook are identical, I copied the modules. 问题是原始工作簿和新工作簿中的宏完全相同,因此我复制了模块。

So my question : Is there a way that I run a macro with an identical name from another workbook in VBA-Excel. 所以我的问题是:有没有一种方法可以运行VBA-Excel中另一个工作簿中具有相同名称的宏。 It is important that this macro will only access macro's from it's own workbook! 重要的是,此宏只能访问其自己的工作簿中的宏!

Thanks in advance 提前致谢

I believe the following demonstrates what you need to do. 我相信以下内容将说明您需要做什么。

I have created two workbooks: "TryDup1.xls" and "TryDup2.xls". 我创建了两个工作簿:“ TryDup1.xls”和“ TryDup2.xls”。

"TryDup1.xls" contains the following code in a module: “ TryDup1.xls”在模块中包含以下代码:

Option Explicit
  Sub CtrlDup()

    Dim PathCrnt As String
    Dim WBookOrig As Workbook
    Dim WBookOther As Workbook

    Call DupMac(1)

    Set WBookOrig = ActiveWorkbook
    PathCrnt = ActiveWorkbook.Path & "\"

    Set WBookOther = Workbooks.Open(PathCrnt & "TryDup2.xls")

    Call Application.Run("TryDup1.xls!DupMac", 2)

    Call Application.Run("TryDup2.xls!DupMac", 3)

    WBookOther.Close SaveChanges:=False

    Call DupMac(4)

End Sub
Sub DupMac(Param As Long)

  Debug.Print "TryDup1's DupMac: Param=" & Param

End Sub

"TryDup2.xls" contains the following code in a module: “ TryDup2.xls”在模块中包含以下代码:

Option Explicit
Sub DupMac(Param As Long)

  Debug.Print "TryDup2's DupMac: Param=" & Param

End Sub

Opening "TryDup1.xls" and executing CtrlDup() causes the following to be output to the Immediate window: 打开“ TryDup1.xls”并执行CtrlDup()会将以下内容输出到立即窗口:

TryDup1's DupMac: Param=1
TryDup1's DupMac: Param=2
TryDup2's DupMac: Param=3
TryDup1's DupMac: Param=4

So macro CtrlDup() can run either version of macro DupMac(). 因此,宏CtrlDup()可以运行任一版本的宏DupMac()。

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

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