I have a template sheet that I copy with the following code where strNameOfSheetToCreate is a string captured from a cell :
Dim Wb As Workbook: Set Wb = ThisWorkbook
Wb.Sheets.Add.Name = strNameOfSheetToCreate
Dim SheetSource As Worksheet
Dim SheetTarget As Worksheet
Set SheetSource = Wb.Worksheets("Project Template")
Set SheetTarget = Wb.Worksheets(strNameOfSheetToCreate)
'copy all formulas and conditional formatting from template
SheetSource.Cells.Copy
SheetTarget.Cells.PasteSpecial Paste:=xlPasteAllMergingConditionalFormats
This copies the sheet correctly but doesn't copy over any of the macro code from the template sheet, such as the activate macro as below:
Private Sub Worksheet_Activate()
Dim ThisSheet As Worksheet
Set ThisSheet = ActiveSheet
ThisSheet.Range("H40").Formula2 = "=FILTER(FILTER(AllStaffProjectAllocationTbl,AllStaffProjectAllocationTbl[Project Code Name]=B2), {1,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0})"
End Sub
Is there a way to also copy over the macro code to the new sheet?