VBA apply code to ThisWorkbook when sheet copied to new workbook

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
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.
 
Upvote 0
Happy you have it sorted but just be careful when coding using VBIDE, it can cause major issues if you get it wrong.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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