I'm wanting to click a button and a specified sheet is copied into a new workbook.
I have managed to create that using the code below.
The below code will never overwrite another similar named file because elsewhere in my VBA a new folder is created, and this Sub saves the sheet to that folder.
The problem I'm facing is if a file called 'Customer Index.xlsx' is already open on the user's computer, the code fails. Is there a way around this issue?
Sub SaveNewWorkbook(path As String)
On Error GoTo ErrorHandler
ThisWorkbook.Worksheets("Customer Index for Scheme").Copy
With ActiveWorkbook
.ActiveSheet.name = "Customer Index"
.ActiveSheet.Shapes("Button 1").Delete
.ActiveSheet.Shapes("Button 2").Delete
.SaveAs filename:=path & "\Customer Index.xlsx", FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
Exit Sub
ErrorHandler:
MsgBox "Unable to create index spreadsheet."
End Sub
Bookmarks