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
17
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.
 

Eagle72

New Member
Joined
Apr 5, 2005
Messages
17
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"
 

Forum statistics

Threads
1,081,560
Messages
5,359,604
Members
400,538
Latest member
leon_oscar

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top