VBA apply code to ThisWorkbook when sheet copied to new workbook

hajiali

Active Member
Joined
Sep 8, 2018
Messages
392
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Sub chkDatesCopyDutyRoster()
Dim ws As Worksheet, Sh As Worksheet
Set ws = ActiveSheet
Set Sh = Sheets("BUILD")
If Sheets("Results").Range("X1") = True Then
    If DateSerial(ws.Range("H1").Value, ws.Range("E1").Value, ws.Range("F1").Value) < Sh.Range("B1").Value Or _
        DateSerial(ws.Range("H1").Value, ws.Range("E1").Value, ws.Range("F1").Value) > Sh.Range("I1").Value Then
        MsgBox "CHANGE THE DATE OF THE LAST DAY OF BID IN SHEET BID RESULTS TO CONTUINE EXPORT"
        Exit Sub
    End If
ElseIf Sheets("Results").Range("X1") = False Then
    If DateSerial(ws.Range("H1").Value, ws.Range("E1").Value, ws.Range("F1").Value) < Sh.Range("AT1").Value Or _
        DateSerial(ws.Range("H1").Value, ws.Range("E1").Value, ws.Range("F1").Value) > Sh.Range("BA1").Value Then
        MsgBox "CHANGE THE DATE OF THE LAST DAY OF BID IN SHEET BID RESULTS TO CONTUINE EXPORT"
        Exit Sub
    End If
End If
Dim wb As Workbook
Set ws = ActiveSheet
ws.Unprotect Password:="262"
ws.Copy
ws.Protect Password:="262"
With ActiveWorkbook
ws.Cells.Copy .Sheets(1).Range("A1")
    ActiveWorkbook.SaveAs fileName:= _
        "c:\" & Range("G1") & "\" & Range("F1") & ".xlsm ", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End With
Call macro1
Call macro2
Call macro3
End Sub

Using the above code I'm able to copy the Active Sheet to its own workbook and save it to directory. I would like to add the function to copy the following code to the new workbooks "ThisWorkbook" so that when the new workbook opens anytime afterwards it runs test1 and test2

VBA Code:
Private Sub Workbook_Open()
    With Sheet1
        Call .test1
        Call .test2
    End With
End Sub

The 2 macros are Public Sub test1() and Public Sub test2() on the sheet that is being copied
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

hajiali

Active Member
Joined
Sep 8, 2018
Messages
392
Office Version
  1. 2016
Platform
  1. Windows
Thanks Mark858 for the concern but I read over the link and was able to accomplish everything that it required and was able to correct all errors.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,034
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Happy you have it sorted but just be careful when coding using VBIDE, it can cause major issues if you get it wrong.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,285
Messages
5,635,314
Members
416,852
Latest member
kanaikls

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
Top