Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default delete all sheets except...

Hi,
I am trying to delete all sheets except 3 (one of
which "admin" has got the names of the 2 other not to
delete in cells that are named "name2" and "name2").
Here's my code but it is falling over. Any idea?
Thanks

Call UnprotectWorkbook

Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If LCase(sh.Name) < "admin" And LCase(sh.Name)
< Range("name1").Value And LCase(sh.Name) < Range
("name2").Value Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default delete all sheets except...

Caroline,

I have changed the code a bit to ensure no wrap-around

Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If (LCase(sh.Name) < "admin" And _
LCase(sh.Name) < Range("name1").Value And _
LCase(sh.Name) < Range("name2").Value) Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True

YHou also would probably be best to qualify the range names by the worksheet
in case that is not active

Worksheets("Sheet1").Range("name1").Value

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"caroline" wrote in message
...
Hi,
I am trying to delete all sheets except 3 (one of
which "admin" has got the names of the 2 other not to
delete in cells that are named "name2" and "name2").
Here's my code but it is falling over. Any idea?
Thanks

Call UnprotectWorkbook

Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If LCase(sh.Name) < "admin" And LCase(sh.Name)
< Range("name1").Value And LCase(sh.Name) < Range
("name2").Value Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default delete all sheets except...


Thanks a lot, Bob. It works.

-----Original Message-----
Caroline,

I have changed the code a bit to ensure no wrap-around

Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If (LCase(sh.Name) < "admin" And _
LCase(sh.Name) < Range("name1").Value And _
LCase(sh.Name) < Range("name2").Value) Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True

YHou also would probably be best to qualify the range

names by the worksheet
in case that is not active

Worksheets("Sheet1").Range("name1").Value

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"caroline" wrote

in message
...
Hi,
I am trying to delete all sheets except 3 (one of
which "admin" has got the names of the 2 other not to
delete in cells that are named "name2" and "name2").
Here's my code but it is falling over. Any idea?
Thanks

Call UnprotectWorkbook

Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If LCase(sh.Name) < "admin" And LCase(sh.Name)
< Range("name1").Value And LCase(sh.Name) < Range
("name2").Value Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default delete all sheets except...

Just another general idea.

Nme1 = UCase([name1])
Nme2 = UCase([name2])
For Each sh In ThisWorkbook.Worksheets
Select Case UCase(sh.Name)
Case "ADMIN", Nme1, Nme2
'Do Nothing
Case Else
sh.Delete
End Select
Next sh

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"caroline" wrote in message
...
Hi,
I am trying to delete all sheets except 3 (one of
which "admin" has got the names of the 2 other not to
delete in cells that are named "name2" and "name2").
Here's my code but it is falling over. Any idea?
Thanks

Call UnprotectWorkbook

Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If LCase(sh.Name) < "admin" And LCase(sh.Name)
< Range("name1").Value And LCase(sh.Name) < Range
("name2").Value Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default delete all sheets except...

A nitpicking detail. Your code assumes that the activesheet is the
ADMIN sheet. It also assumes that the names in the cells are in lower
case. If either of these conditions is not true (and Option Compare
Binary is set), the result could be disastrous.

An untested alternative:

Option Explicit

Sub testIt()
Dim Protect1 As String, Protect2 As String, WS As Worksheet
With Worksheets("admin")
Protect1 = UCase(.Range("name1").Value)
Protect2 = UCase(.Range("name2").Value)
End With
For Each WS In ThisWorkbook.Worksheets
Select Case UCase(WS.Name)
Case "ADMIN", Protect1, Protect2
'Do Nothing
Case Else
WS.Delete
End Select
Next WS
End Sub

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Hi,
I am trying to delete all sheets except 3 (one of
which "admin" has got the names of the 2 other not to
delete in cells that are named "name2" and "name2").
Here's my code but it is falling over. Any idea?
Thanks

Call UnprotectWorkbook

Dim sh As Worksheet
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Worksheets
If LCase(sh.Name) < "admin" And LCase(sh.Name)
< Range("name1").Value And LCase(sh.Name) < Range
("name2").Value Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True



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-delete all sheets except puiuluipui Excel Discussion (Misc queries) 6 October 13th 09 05:49 AM
Delete Sheets Jahsonn Excel Programming 1 April 26th 04 01:42 PM
Delete Sheets SS[_3_] Excel Programming 2 February 5th 04 02:37 PM
Delete new sheets? ianripping[_27_] Excel Programming 1 January 17th 04 02:14 PM
select and delete sheets Locachica Excel Programming 5 December 31st 03 01:02 PM


All times are GMT +1. The time now is 12:28 AM.

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"