Copy module to new workbook, create a button in the new workbook and assigns a new workbook macro

Eagle72

New Member
Joined
Apr 5, 2005
Messages
21
Hi Everyone,

I am copying a module from one workbook to another so that in the future, the new workbook can run a macro from within itself rather than referring to an external workbook (which may or may not be open in the future). Then I create a new workbook by moving a sheet from one to the other and create a new button in the new workbook. That button is supposed to run a macro in that new workbook. The problem is that the macro assigned to the button refers to the macro from the original workbook, not the new one. I'm running Windows 7 and Excel 2013.

Code:
Sub Macro1()
    ActiveWorkbook.VBProject.VBComponents("module1").Export ("F:\Module11.bas")
    ActiveSheet.Move
    ActiveWorkbook.VBProject.VBComponents.Import ("F:\Module11.bas")
    ActiveSheet.Buttons.Add(410.6, 17.4, 213, 35.2).Select
    Selection.OnAction = "ButtonTest"
End Sub

If my original workbook was named Book2, the button in the new workbook has this macro assigned to it: 'Book2!ButtonTest' rather than the native 'ButtonTest'.

Please help.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Eagle72

New Member
Joined
Apr 5, 2005
Messages
21
Hi Everyone,

I am copying a module from one workbook to another so that in the future, the new workbook can run a macro from within itself rather than referring to an external workbook (which may or may not be open in the future). Then I create a new workbook by moving a sheet from one to the other and create a new button in the new workbook. That button is supposed to run a macro in that new workbook. The problem is that the macro assigned to the button refers to the macro from the original workbook, not the new one. I'm running Windows 7 and Excel 2013.

Code:
Sub Macro1()
    ActiveWorkbook.VBProject.VBComponents("module1").Export ("F:\Module11.bas")
    ActiveSheet.Move
    ActiveWorkbook.VBProject.VBComponents.Import ("F:\Module11.bas")
    ActiveSheet.Buttons.Add(410.6, 17.4, 213, 35.2).Select
    Selection.OnAction = "ButtonTest"
End Sub

If my original workbook was named Book2, the button in the new workbook has this macro assigned to it: 'Book2!ButtonTest' rather than the native 'ButtonTest'.

Please help.

I found a solution. replacing the Selection.OnAction command with the one below solves the problem:
Code:
Selection.OnAction = ActiveWorkbook.Name & "!ButtonTest"
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,641
Messages
5,832,829
Members
430,169
Latest member
sartemiou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top