Insert a macro in an about-to-be-created workbook

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 2010
I have a macro that spins off a specific sheet from a workbook and saves it in a given path.
Before saving it, I'd like to have a macro placed in this about-to-be-saved workbook.
I have the relatively short code for the macro to be place in it, just don't know what I should do to go about this association with the new workbook.
 
Here is an example that uses two workbooks. source.xlsm and target.xlsm.
The following code assumes that they are both in the same path. All code goes in the workbook classes.
See 'kweaver' folder that contains two example file here. The example uses a shape with an assigned/re-assigned macro in addition to OnKey.


In source.xlsm workbook class module. The one that does the formatting and spawns worksheets.
Perform your formatting before calling.
VBA Code:
Sub FormatAndCopyToTargetWorkbook()
    With Workbooks.Open(Path & "\target.xlsm")
        ActiveSheet.Copy After:=.Sheets(1)
        Application.DisplayAlerts = False
        .Sheets(1).Delete 'delete the blank sheet in target
        Application.DisplayAlerts = True
        .Sheets(1).Name = Format(Date, "YY.MM.DD") 'you mentioned a date stamp?
        .SaveCopyAs Application.GetSaveAsFilename(, "Macro Enabled Workbook (*.xlsm), *.xlsm", , "Save Destination Workbook")
        .Close False
    End With
End Sub

In target.xlsm workbook class module.
This is the workbook that will receive the copied worksheet.
VBA Code:
Private Sub Workbook_Activate()
    Application.OnKey "+^{C}", Me.CodeName & ".InsertCopyRow"
End Sub

Private Sub Workbook_Deactivate()
    Application.OnKey "+^{C}", vbNullString
End Sub

Public Sub InsertCopyRow()
    MsgBox "InsertCopyRow code runs here"
End Sub
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Thank you very much. I'm having some difficulty adapting what you suggested.
What I need to do is copy the sheet called "TimeSheet" to the new xlsm file which I've created and saved as "ReformattedFile.xlsm".

What adjustment(s) do I need to make?
 
Upvote 0
Dataluver: THANK YOU!!! for all of your help. After a good sleep I worked through your code and have been successful implementing what I needed.
Further attests to the fantastic help in this forum.

Cheers!

Kevin
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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