Hello,
I'm am trying to make a copy of a workbook, which I then open and then I want to delete all sheets except for the sheet "OrderformulierKopie". The code I'm using right now keeps giving the error it is out of range (in the for loop). I can't figure out how to fix it.
Ivlgnr is made in another sub, defined like this: Ivlgnr = "DIS" & Format(Now(), "yy") & "-" & Format(volgnummer, "0000")
Any advice is welcome!
I'm am trying to make a copy of a workbook, which I then open and then I want to delete all sheets except for the sheet "OrderformulierKopie". The code I'm using right now keeps giving the error it is out of range (in the for loop). I can't figure out how to fix it.
Ivlgnr is made in another sub, defined like this: Ivlgnr = "DIS" & Format(Now(), "yy") & "-" & Format(volgnummer, "0000")
Any advice is welcome!
Code:
Private Sub cmdStart_Click()
mkFolder (ThisWorkbook.Path & "\" & Ivlgnr)
Dim CopyNaam As String
CopyNaam = ThisWorkbook.Path & "\" & Ivlgnr & "\" & Ivlgnr & ".xlsm"
ActiveWorkbook.SaveCopyAs CopyNaam
Application.Workbooks.Open CopyNaam
Dim iterator As Long
Application.DisplayAlerts = False
For iterator = Workbooks(CopyNaam).Worksheets.Count To 1 Step -1
With Workbooks(CopyNaam).Worksheets(iterator)
If .CodeName <> "OrderformulierKopie" Then .Delete
End With
Next iterator
Application.DisplayAlerts = True
ThisWorkbook.Save
End
End Sub
Last edited: