ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   sub to delete all sheets other than x, y, z (https://www.excelbanter.com/excel-programming/403457-sub-delete-all-sheets-other-than-x-y-z.html)

Max

sub to delete all sheets other than x, y, z
 
Looking for a sub which would delete all sheets
other than 3 sheets named as: x, y, z. Thanks.



Gary Keramidas

sub to delete all sheets other than x, y, z
 
here's one way, give this a try:

Option Explicit

Sub remove_some_sheets()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name < "x" And ws.Name < "y" And ws.Name < "z" Then
ws.Delete
End If
Next
Application.DisplayAlerts = True
End Sub

--


Gary


"Max" wrote in message
...
Looking for a sub which would delete all sheets
other than 3 sheets named as: x, y, z. Thanks.




JLGWhiz

sub to delete all sheets other than x, y, z
 
Sub delShts()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If sh.Name < "x" And sh.Name < "y" And sh.Name < "z" Then
sh.Delete
End If
Next
End Sub


"Max" wrote:

Looking for a sub which would delete all sheets
other than 3 sheets named as: x, y, z. Thanks.




Max

sub to delete all sheets other than x, y, z
 
Works well, Gary. Thanks.



Max

sub to delete all sheets other than x, y, z
 
Thanks, JLGWhiz
It works well too, after I inserted the lines:
Application.DisplayAlerts = False
....
Application.DisplayAlerts = True
(took the cue from Gary's sub)



Chip Pearson

sub to delete all sheets other than x, y, z
 
Try something like the following. In this code, the sheet names must be in
UPPER CASE unless you have an "Option Compare Text" statement at the top of
the module (before and outside of any procedures), in which case the case of
the name doesn't matter.

Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
Select Case UCase(WS.Name)
Case "X", "Y", "Z"
' do nothing
Case Else
Application.DisplayAlerts = False
WS.Delete
Application.DisplayAlerts = True
End Select
Next WS


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Max" wrote in message
...
Looking for a sub which would delete all sheets
other than 3 sheets named as: x, y, z. Thanks.



Max

sub to delete all sheets other than x, y, z
 
Thanks, Chip ! That works well, too.




All times are GMT +1. The time now is 02:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com