Copy sheet to new workbook - remove Buttons and VBA

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,074
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
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,003
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:

Forum statistics

Threads
1,081,574
Messages
5,359,704
Members
400,545
Latest member
Damntheman30

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top