VBA Assign Button to Macro in a different workbook

bressler4534

New Member
Joined
Aug 9, 2022
Messages
1
Hello, I have created a template spreadsheet containing a button form control in my main workbook "Book1" and have written a macro titled "Copy" in Book1 to copy the template sheet to Book2. In the same macro titled "Copy" I have also been able to copy "Module5" from Book1 to Book2.

My goal is to create a macro in Book1 that will link the button in Book2 to Module5 in Book2. That way I do not have to click assign macro in Book2 every time I run "Copy". I have tried various VBA codes on other forums with no luck. Is this possible to do through a macro located in Book1 and if so, is there a generic code to do so? If anyone is able to assist I would greatly appreciate your help. Thank you!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I have a routine where the code in the first workbook allows you to select an open workbook to affect and sets it to a workbook variable named "Import1" (although, you could hard code open the workbook with a hardcoded name, if that's what you need). It sets that workbook active (Import1.Activate). Then I can do anything to that workbook, just as I would with the workbook the code is in.
VBA Code:
'at top of Module to share across all Subs and Userforms
Global Import1 As Workbook

'In userform code to select open workbook
Set Import1 = Workbooks(frmImport.lstWorkbooks.Value)

'in the Sub you want to make things happen
Import1.Activate
With Import1
     .Sheets(1).Columns(4).NumberFormat = "0"
     'etc.
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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
Back
Top