Deleting Worksheets except for one, in Copy of workbook

KlaasE

New Member
Joined
Nov 4, 2019
Messages
37
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!
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:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You can only use the sheet codename like that in the workbook containing the code.
Try using the sheet name instead.
 
Upvote 0
Why not just copy the one sheet you do want, so it creates a new workbook, then save it?
 
Upvote 0
Do you mean I should change this:
Code:
If .CodeName <> "OrderformulierKopie" Then .Delete
to this?
Code:
If .Name <> "OrderformulierKopie" Then .Delete
This still gives me the same error
 
Last edited:
Upvote 0
You have an extra " which shouldn't be there, but is that the actual name of the sheet, as seen on the tab?
Or follow Rorya's advice.
 
Last edited:
Upvote 0
The error is because copynaam has the full file path and you can't use that as an index into the Workbooks collection. You could do this:

Code:
Private Sub cmdStart_Click()
   mkFolder (ThisWorkbook.Path & "\" & Ivlgnr)

   Dim CopyNaam As String
   CopyNaam = ThisWorkbook.Path & "\" & Ivlgnr & "\" & Ivlgnr & ".xlsm"

   ActiveWorkbook.SaveCopyAs CopyNaam

   Dim NewBook As Workbook
   Set NewBook = Application.Workbooks.Open(CopyNaam)

   Application.DisplayAlerts = False
   
   Dim theSheet As Object
   For Each theSheet In NewBook.Sheets
      If LCase$(theSheet.CodeName) <> "orderformulierkopie" Then theSheet.Delete
   Next
   Application.DisplayAlerts = True


   ThisWorkbook.Save

End Sub
 
Upvote 0
And there was me thinking you could only directly refer to the codename in ThisWorkbook.
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,668
Members
452,992
Latest member
TokugawaIesuma

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