Disabling All VBA code from worksheet and/or workbook

hazmat

New Member
Joined
Jun 14, 2019
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
I have the following code that will copy sheet1 to a closed workbook. Once the sheet is copied to the Target Workbook, I don't want the code to run there.
The Target WorkBook will periodically have new sheet's added from the Source WorkBook.

Is there either
A) Code that can be added to this sub routine to Disable the code, once the sheet is copied to the closed Workbook, or
B) Something i can add to the Target Workbook directly, to disable all the code there, or
C) Change something in this code to only send the Sheet values to the Target WorkBook

Thanks

VBA Code:
Private Sub CopySheetToClosedWorkbook()

    Dim sourceWs As Worksheet
    Dim targetWb As Workbook
    Dim targetWs As Worksheet
    Dim targetWbPath As String
    
    ' Set the source worksheet you want to copy from the active workbook
    Set sourceWs = Sheet1
    
    ' Define the path to the target workbook
    targetWbPath = "C:\Closed FireFighter OT Rounds 2024.xlsm"

    ' Disbales screen updating
    Application.ScreenUpdating = False
    
    ' Open the target workbook behind the scenes
    Set targetWb = Workbooks.Open(targetWbPath)
    
    ' Copy the worksheet to the target workbook
    sourceWs.Copy After:=targetWb.Sheets(targetWb.Sheets.Count) 'Add something here to only copy the values???
    
    ' Save and close the destination workbook
    targetWb.Close SaveChanges:=True
    
    ' Enables screen updating
    Application.ScreenUpdating = True

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
When you close the workbook. Save as xl not xlsm.
This will not allow any scripts to run.
 
Upvote 0
When you close the workbook. Save as xl not xlsm.
This will not allow any scripts to run.
Thanks for your response.
Although saving as .xl didn't work for me, it did lead me to a solution.
I saved my Target Workbook as .xlsx and added 'Application.DisplayAlerts = False' before copying, to take care of the alert that pops up when saving the .xlsm worksheet to a .xlsx file.
 
Upvote 0
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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