Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default 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.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default 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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default sub to delete all sheets other than x, y, z

Works well, Gary. Thanks.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default 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.



  #5   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default 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)




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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.


  #7   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default sub to delete all sheets other than x, y, z

Thanks, Chip ! That works well, too.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to delete sheets and saves remaining file does not properly delete module pherrero Excel Programming 0 June 21st 05 05:16 PM
Macro to delete sheets and saves remaining file does not properly delete module pherrero Excel Programming 0 June 21st 05 05:11 PM
Macro to delete sheets and saves remaining file does not properly delete module bhawane Excel Programming 0 June 21st 05 04:54 PM
Macro to delete sheets and saves remaining file does not properly delete module bhawane Excel Programming 0 June 21st 05 04:53 PM
Macro to delete sheets and saves remaining file does not properly delete module pherrero Excel Programming 0 June 21st 05 04:38 PM


All times are GMT +1. The time now is 06:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"