Copying sub workbook macros across

Corleone

Well-known Member
Joined
Feb 2, 2003
Messages
837
Office Version
  1. 365
I have a macro in my spreadhseet which selects certain tabs from the master file and creates a separate copy of the spreadhseet containing just these selected tabs.

within the master file i have a private sub workbook macro.

the issue i have is when i create my copy of the master spreadsheet, it copies all the worksheet macros but not the workbook macro

is there any way around this?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I have a macro in my spreadhseet which selects certain tabs from the master file and creates a separate copy of the spreadhseet containing just these selected tabs.

within the master file i have a private sub workbook macro.

the issue i have is when i create my copy of the master spreadsheet, it copies all the worksheet macros but not the workbook macro

is there any way around this?
Alternativly, is their a way of creating a worksheet event code which is activiated when the file is saved?
 
Upvote 0
You could make a macro that saves your current workbook with a new name and deletes the sheets you don't want. This way any workbook macros are kept.
Code:
Sub MySaveAs()
    
    Application.DisplayAlerts = False
        Sheets("Sheet2").Delete
        Sheets("Sheet4").Delete
    Application.DisplayAlerts = True
    ThisWorkbook.SaveAs Filename:="MyFile"
    
End Sub

There is also the Workbook_BeforeSave event macro. This is located in the ThisWorkbook module.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
   ' Your code here
End Sub
 
Upvote 0
You could make a macro that saves your current workbook with a new name and deletes the sheets you don't want. This way any workbook macros are kept.
Code:
Sub MySaveAs()
    
    Application.DisplayAlerts = False
        Sheets("Sheet2").Delete
        Sheets("Sheet4").Delete
    Application.DisplayAlerts = True
    ThisWorkbook.SaveAs Filename:="MyFile"
    
End Sub

There is also the Workbook_BeforeSave event macro. This is located in the ThisWorkbook module.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
   ' Your code here
End Sub

Many thanks for this - i will have a look at it in the morning
 
Upvote 0
Prevent user from saving file with filtered data

m
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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