简体   繁体   中英

Create a new workbook, import Macro and assign button

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? ie destwb."Test_Button_Press" sort of thing.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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