Copy sheet to new workbook - remove Buttons and VBA

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,428
Im wanting to copy many sheets from my workbook and save then into a new workbook to act as a backup.

current code.

Code:
Sub Save_Backup_File()
Application.ScreenUpdating = False

Dim wbSave As Workbook
Set wbSave = ThisWorkbook

Dim wbNew As Workbook

myPath = "C:\Results\"
myfile = myPath & "Scoring Tower Backup " & Format(Now, "mm-dd-yy") & ".xlsx"

Set wbNew = Workbooks.Add

For Each ws In wbSave.Sheets
 If ws.Visible = xlSheetVisible And ws.[A1] = "XXX" Then
    ws.Copy after:=wbNew.Sheets(wbNew.Sheets.count)
 End If
Next

wbNew.SaveAs FileName:=myfile
ActiveWorkbook.Close
Application.ScreenUpdating = True
End Sub

this code works but I have 2 issues.

1. The original worksheets have buttons on them assigned to code. I need these buttons removed in the backup file.
2. The original sheets have code that runs on sheet activation and cell change events that call other macros, therefor I get errors as this code is not in the new workbook (nor do I want it to be)


I am only really after the sheet values and formatting. to be copied.

Thanks for looking,

Ross
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
After you've copied your worksheets to your new workbook and before saving your new workbook, loop through each worksheet within your new workbook and delete any and all shapes...

Code:
    Dim oShape As Shape

    For Each ws In wbNew.Worksheets
        For Each oShape In ws.Shapes
            oShape.Delete
        Next oShape
    Next ws

Then, when saving your new workbook, specify the file format...

Code:
    Application.DisplayAlerts = False
    wbNew.SaveAs Filename:=myfile, FileFormat:=51 'xlOpenXMLWorkbook
    Application.DisplayAlerts = True

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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