Query re: macros and copying worksheets

scarlett3

Board Regular
Joined
Jan 21, 2005
Messages
73
Office Version
  1. 365
Platform
  1. Windows
I have two Excel files and both have the same macro entitled 'sort'. There is a button on each worksheet that the user can press and it will sort the data in that worksheet using that macro.

Here is the problem...

I need to copy some of the worksheets from Excel file A (my own personal use) to Excel file B (for other people to use). However, the button on the copied over worksheets is assigned to the 'sort' macro in Excel file A. I need to manually edit the button in each worksheet to assign it to the 'sort' macro in Excel file B.

Is there a way for the worksheets to be copied over and the button on each worksheet to be assigned to the macro in the destination file, not the original file?

Thanks for your help!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
scarlett3,

Below is one approach to copy the ActiveSheet to another workbook...

VBA Code:
Sub CopySheetAndButton1()
Dim wb1 As Workbook
Set wb1 = Workbooks("ExcelFileB.xlsm")
ActiveSheet.Copy before:=wb1.Sheets(1)
wb1.Sheets(1).Buttons(1).OnAction = wb1.Name & "!MacroName"
End Sub

The code will also assign the macro in the destination file to the button. (I'm assuming there's only one button on the sheet.)

Cheers,

Tony
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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