delete all but first 5 tabs in a workshhet

ockie

New Member
Joined
Jan 11, 2020
Messages
36
Office Version
  1. 2013
Platform
  1. Windows
Hi,
on a worksheet I am trying to delete all but the first 5 tabs, named, "finish positions" " club champos" " committee" "master" "template"
The tabs after these 5 will regularly have different names and there could be up to 25 tabs that need deleting on a regular basis.

hoping someone can help, thanks in advance
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
@ockie Test this and see if it does the trick.
I have used the names rather than tab number, just in case.

VBA Code:
Sub Del_Bar_5()

Response = MsgBox("Are yiu sure  you wish to delete tabs?", vbYesNo)

If Response = vbNo Then Exit Sub

Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sht In ThisWorkbook.Sheets
    Select Case sht.Name
    Case "finish positions", "club champos", "committee", "master", "template"
    
    Case Else
    sht.Delete
    
    End Select
Next sht
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Hope that helps.
 
Upvote 0
Solution
@ockie Test this and see if it does the trick.
I have used the names rather than tab number, just in case.

VBA Code:
Sub Del_Bar_5()

Response = MsgBox("Are yiu sure  you wish to delete tabs?", vbYesNo)

If Response = vbNo Then Exit Sub

Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sht In ThisWorkbook.Sheets
    Select Case sht.Name
    Case "finish positions", "club champos", "committee", "master", "template"
   
    Case Else
    sht.Delete
   
    End Select
Next sht
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Hope that helps.
Hi Snakehips, the code works too well, it has deleted "committee", "master", "template" as well as the ones it was supposed to. thanks for trying
 
Upvote 0
Hi Snakehips, the code works too well, it has deleted "committee", "master", "template" as well as the ones it was supposed to. thanks for trying
Are the tab names as per the names in the code? Do your actual tab names have leading or trailing spaces????
 
Upvote 0
Are the tab names as per the names in the code? Do your actual tab names have leading or trailing spaces????
Hi Snakehips, even from a distance you were able to pick up an error, the 3 tabs that were deleted had a capital letter in the name, I have changed them and IT WORKS.
thanks for your help, much appreciated.
 
Upvote 0
You can avoid problems with upper/lower case like
VBA Code:
Select Case LCase(sht.Name)
Case "finish positions", "club champos", "committee", "master", "template"
 
Upvote 0
You can avoid problems with upper/lower case like
VBA Code:
Select Case LCase(sht.Name)
Case "finish positions", "club champos", "committee", "master", "template"
thanks Fluff, it works.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,560
Members
449,089
Latest member
Motoracer88

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