创建一个新的工作簿,导入宏并分配按钮 [英] Create a new workbook, import Macro and assign button

查看:147
本文介绍了创建一个新的工作簿,导入宏并分配按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些VBA代码来创建新的工作簿,生成一些工作表元素并导入一个代码模块。

I've created some VBA code to create new workbook, spawn some worksheet elements and import a code module.

然后我想使用这些导入的模块将按钮链接到新创建的文件中的宏。

Then I want to link the button to the macros in the newly created file using those imported modules.

相反,VBA正在尝试将新创建的文件的按钮分配给运行代码的工作簿中的宏,而不是目标文件本身。

Instead VBA is trying to assign the button of the newly created file to the macros in the workbook running the code, rather than the destination file itself.

我正在使用一行:

destwb.Worksheets(newSheetName).Shapes("Button 1").OnAction = "Test_Button_Press"

其中 destwb 是新的工作簿对象和 newSheetName 是这本新书中包含这些元素的工作表的名称。

Where destwb is the new workbook object and newSheetName is the name of the sheet in this new book containing these elements.

有些代码可以使用替换Test_Button_Press的工具,然后是其代号?

Is there some code that I can use to replace "Test_Button_Press" with something more explicit of the workbook then its codename? i.e. destwb."Test_Button_Press" sort of thing.

推荐答案

尝试使用:

destwb.Worksheets(newSheetName).Shapes("Button 1").OnAction = destwb.Name & "!Test_Button_Press"

使OnAction字符串包含新工作簿的名称,感叹号和然后是宏的名称。

So that the OnAction string includes the name of the new workbook, an exclamation mark and then the name of the macro.

这篇关于创建一个新的工作簿,导入宏并分配按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆