Delete sheets if they are not included in a list. (Not a list range)

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
61
Hello everyone. I'm trying to delete sheets that are not included in a list. I've found some posts that explain how to delete sheets if they are not included in a list, ie an actual range cells A1:A10. However, I would like to delete the sheets listed within the VBA code not a listed range.

I have a some python code below that I would like to replicate in VBA.

Code:
[COLOR=#303336][FONT=inherit]wb [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] openpyxl[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]load_workbook[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'testdel.xlsx'[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR]<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">[COLOR=#303336][FONT=inherit]ws [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]get_sheet_names[/FONT][/COLOR][COLOR=#303336][FONT=inherit]()[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
[/FONT][/COLOR][COLOR=#101094][FONT=inherit]for[/FONT][/COLOR][COLOR=#303336][FONT=inherit] sheetName [/FONT][/COLOR][COLOR=#101094][FONT=inherit]in[/FONT][/COLOR][COLOR=#303336][FONT=inherit] ws[/FONT][/COLOR][COLOR=#303336][FONT=inherit]:[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]if[/FONT][/COLOR][COLOR=#303336][FONT=inherit] sheetName [/FONT][/COLOR][COLOR=#101094][FONT=inherit]not[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]in[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#303336][FONT=inherit][[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet25'[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet50'[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet75'[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet100'[/FONT][/COLOR][COLOR=#303336][FONT=inherit]]:[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
        sheetToDelete [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]get_sheet_by_name[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]sheetName[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#303336][FONT=inherit]//[/FONT][/COLOR][COLOR=#303336][FONT=inherit] gets the sheet object
        wb[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]remove_sheet[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]sheetToDelete[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit] 
wb.save('testdel2.xlsx')[/FONT][/COLOR]</code>

Any ideas or suggestions on how to accomplish this using VBA?

Thanks in advance for your help.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This should delete the sheets
Code:
Sub DelSheets()

    Dim Arr As Variant
    Dim Sht As Variant
    
    Arr = Array("Sheet1", "Sheet2", "Sheet3")
    
Application.DisplayAlerts = False
    For Each Sht In Arr
        Sheets(Sht).Delete
    Next Sht
Application.DisplayAlerts = True

End Sub
 
Upvote 0
This does delete the sheets listed. However, I'm trying to delete all the sheets that are not listed. For example, if I have sheet1 - sheet100 and only need to keep Sheet25, Sheet50, Sheet75, and Sheet100 I would like to add those to the VBA list and delete all other tabs.
 
Upvote 0
I saw this part of your op
However, I would like to delete the sheets listed within the VBA code not a listed range.
& ignored the previous part :oops:
Try this instead
Code:
Sub DelSheets()

    Dim Arr As Variant
    Dim Sht As Worksheet
    
    Arr = Array("Sheet1", "Sheet2", "Sheet3")
    
Application.DisplayAlerts = False
    For Each Sht In Worksheets
        If Not UBound(Filter(Arr, Sht.Name, True, vbTextCompare)) >= 0 Then Sht.Delete
    Next Sht
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Here is an alternative.

Code:
Sub DSITANIIAL()
Dim Sh As Worksheet
     
 With Application
 .ScreenUpdating = False
 .DisplayAlerts = False
 End With
    For Each Sh In ActiveWorkbook.Worksheets
          Select Case Sh.Name
               Case "[COLOR=#ff0000]Sheet1[/COLOR]", "[COLOR=#ff0000]Sheet2[/COLOR]", "[COLOR=#ff0000]Sheet3[/COLOR]"
               [COLOR=#009900]'If it matches it won't do anything[/COLOR]
               Case Else
               Sh.Delete
           End Select
   Next Sh
 With Application
 .ScreenUpdating = True
 .DisplayAlerts = True
 End With
 End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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