简体   繁体   English

打开新工作簿,然后使用按钮运行宏

[英]Open new workbook, and run macro, using button

I have a workbook that runs a large multitude of macros, all based on which button is clicked. 我有一本运行大量宏的工作簿,所有这些宏均基于单击的按钮。

I need one of the macro to open a new workbook, and import two files into the workbook (that part I can do). 我需要一个宏来打开一个新的工作簿,并将两个文件导入到工作簿中(我可以做到这一点)。

The second part is that I need it to run a particular macro, in the new workbook. 第二部分是我需要它在新工作簿中运行特定的宏。 The macro would be stored in the original workbook . 该宏将存储在原始工作簿中

I've seen some suggestions that I need to use APPLICATION.RUN , which has the parameters of having to choose a workbook and name of the routine. 我已经看到一些需要使用APPLICATION.RUN建议,该参数具有必须选择工作簿和例程名称的参数。

Does this method work? 这种方法行得通吗?

Assuming it does: 假设确实如此:

For the workbook, do I need to specify the file path , or just the workbook name? 对于工作簿,我需要指定文件路径 ,还是仅指定工作簿名称?

For the macro, do I need to make the macro public ? 对于宏,我需要将宏公开吗?

Do I need to specify the module it is in? 我需要指定它所在的模块吗?

Are there any other parameters I need to specify to get this method to work? 我需要指定其他参数才能使此方法正常工作吗?

Is there another method that might work , if APPLICATION.RUN does not work? 如果APPLICATION.RUN不起作用,是否还有另一种方法可以起作用?

What you need to use is : 您需要使用的是:

Application.Run "'FileName.xlsm'!MacroName", "Parameters"

You don't need to specify the path if your workbook is already open, you don't need to specify the module as you can't have doubloons names for different procedures, and there are no other parameters needed . 如果您的工作簿已经打开,则无需指定路径也不需要指定模块,因为您没有用于不同过程的双引号名称, 也不需要其他参数

The procedure doesn't need to be public (let me know if it does), and for alternative methods in VBA, there is none (as the Call method only work in the same workbook), but there are some in VB if this doesn't work out. 过程不需要公开 (让我知道它是否公开 ),并且对于VBA中的替代方法,没有任何方法(因为Call方法仅在同一工作簿中起作用),但在VB中有一些方法(如果没有)没办法。

Here is a very short example that you can adapt: 这是一个可以适应的简短示例:

Sub demo()
    Dim Original As Workbook
    Dim Created As Workbook
    Set Original = ThisWorkbook

    '   create a new workbook

    Workbooks.Add
    Set Created = ActiveWorkbook

    '    go back

    Original.Activate

    'make a change in the newly created workbook

    Created.Sheets("Sheet1").Range("A1").Value = "whatever"

    '   save the newly created workbook

    Created.Save
    Created.Close

End Sub

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

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