VBA Copy workbook as .xlsm but delete macro

Kimchi

New Member
Joined
May 8, 2022
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Hello,

I need to write a code that's supposed to automatically refresh a Query, then save the current workbook and close it when opening the file.
The problem is, the file also needs to be sent out. So I want to make a copy of the file that doesn't run the private sub. However, it does have other macro's it can run so I found out that saving it as .xlsx doesn't work. However, when saved as .xlsm the private sub automatically runs. Any idea's how to bypass this? Current code:

VBA Code:
Sub Test() 
Dim x As String, y As String 
x = ActiveWorkbook.Path
y = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) 

Application.DisplayAlerts = False 
Application.ScreenUpdating = False 

Workbooks("Quantity en revenue overview all partners Hardcopy v3 - Copy (2).xlsm").Activate 
ActiveWorkbook.Connections("Query - Details").Refresh 
ActiveWorkbook.SaveAs x & "" & y & "_" & Format(Date, "ddmmyyyy") & ".xlsx", FileFormat:=xlOpenXMLWorkbook
Workbooks("Quantity en revenue overview all partners Hardcopy v3 - Copy (2).xlsm").Close SaveChanges:=True 
Application.ScreenUpdating = True 
End Sub
If anything needs clarification please let me know.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Note: The Query is in workbook A and workbook A will be automatically opened every morning. The idea is that it will refresh the query in workbook A, then save a copy of workbook A without the private sub (the sub is currently not private yet since I'm testing it out).
 
Upvote 0
Note2: I also get the error Subscript out of range at the end, however it does seem that the macro fully does it's job before giving the error.
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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