Create a new workbook, import Macro and assign button

こ雲淡風輕ζ 提交于 2020-01-17 04:40:09

问题


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.

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.

I'm using a line:

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

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

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.


回答1:


Try using:

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

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



来源:https://stackoverflow.com/questions/30317817/create-a-new-workbook-import-macro-and-assign-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!