Export sheet, save as new sheet. Efficiently

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I have the below code that works. But I wanted to know if there was a more efficient way of doing what I am trying to accomplish or a correct way I should say. All I am trying to do is export out a sheet to a new workbook removing all formulas and macro buttons then saving as.

VBA Code:
'Export file, set filters and save-as
Sheets("COMPARE").Copy
Sheets("COMPARE").Unprotect
Sheets("COMPARE").Cells.Copy
Range("A1").Select
ActiveCell.PasteSpecial xlPasteValues
ActiveSheet.Buttons.Delete
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=True
ActiveWorkbook.SaveAs Filename:="C:\my folder.xlsm"
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Maybe with the following it will be a little faster.

VBA Code:
Sub copysheet()
  'Export file, set filters and save-as
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  
  Sheets("COMPARE").Copy
  With ActiveSheet
    .Unprotect
    .Cells.Copy
    .Range("A1").PasteSpecial xlPasteValues
    .Buttons.Delete
    .Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=True
  End With

  With ActiveWorkbook
    .SaveAs Filename:="C:\my folder\my file.xlsx"
    .Close False
  End With
  
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
Tha
Maybe with the following it will be a little faster.

VBA Code:
Sub copysheet()
  'Export file, set filters and save-as
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
 
  Sheets("COMPARE").Copy
  With ActiveSheet
    .Unprotect
    .Cells.Copy
    .Range("A1").PasteSpecial xlPasteValues
    .Buttons.Delete
    .Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=True
  End With

  With ActiveWorkbook
    .SaveAs Filename:="C:\my folder\my file.xlsx"
    .Close False
  End With
 
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic

End Sub
thank you i will put it to use and let you know! i am sure it will work great. much appreciated
 
Upvote 0
Tha

thank you i will put it to use and let you know! i am sure it will work great. much appreciated
One slight problem i am trying to saveas the workbook in .xlsm but receiving this error message

1600083532470.png
VBA Code:
    .SaveAs Filename:="C:\my folder\my file.xlsx"
 
Upvote 0
Change this line:

VBA Code:
.SaveAs Filename:="C:\my folder\my file.xlsx"

for this:
VBA Code:
.SaveAs "C:\my folder\my file.xlsm", xlOpenXMLWorkbookMacroEnabled
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Try this

VBA Code:
Sub copysheet()
  'Export file, set filters and save-as
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  Application.DisplayAlerts = False
  
  Sheets("COMPARE").Copy
  With ActiveSheet
    .Unprotect
    .Cells.Copy
    .Range("A1").PasteSpecial xlPasteValues
    .Buttons.Delete
    .Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=True
  End With

  With ActiveWorkbook
    .SaveAs "C:\my folder\my file.xlsm", xlOpenXMLWorkbookMacroEnabled
    .Close False
  End With
  
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
Try this

VBA Code:
Sub copysheet()
  'Export file, set filters and save-as
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  Application.DisplayAlerts = False
 
  Sheets("COMPARE").Copy
  With ActiveSheet
    .Unprotect
    .Cells.Copy
    .Range("A1").PasteSpecial xlPasteValues
    .Buttons.Delete
    .Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=True
  End With

  With ActiveWorkbook
    .SaveAs "C:\my folder\my file.xlsm", xlOpenXMLWorkbookMacroEnabled
    .Close False
  End With
 
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic

End Sub
It is asking me with a dialog box if i want to replace the file that exists. is there a way to just auto save it over?
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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