PC Review


Reply
Thread Tools Rate Thread

Deleting Sheets Based on Name

 
 
The Boondock Saint
Guest
Posts: n/a
 
      21st Aug 2005
Ive got a macro which makes a large number of sheets with information in
them....

Ive then got another macro which gets all the information from those
sheets....

the problem is... im then left with all the sheets.. and I really want to
delete them.....

Is there anyway to make a macro which would let me delete sheets.. based on
their name.....

all the ones i want to delete are called "cats (104)" or "cats
(105)" etc etc..... there is one called "cats" which i want to keep.... so
basically If it could someonie delete based on the term "cats ("

any ideas?


 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      21st Aug 2005
One way:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim ErrCounter As Long

ErrCounter = 0

On Error Resume Next
Application.DisplayAlerts = False
For Each wks In ActiveWorkbook.Worksheets
If LCase(wks.Name) Like "cats (*" Then
wks.Delete
If Err.Number <> 0 Then
ErrCounter = ErrCounter + 1
Err.Clear
End If
End If
Next wks
Application.DisplayAlerts = True
On Error GoTo 0

If ErrCounter > 0 Then
MsgBox ErrCounter & " sheets were not deleted"
End If

End Sub



The Boondock Saint wrote:
>
> Ive got a macro which makes a large number of sheets with information in
> them....
>
> Ive then got another macro which gets all the information from those
> sheets....
>
> the problem is... im then left with all the sheets.. and I really want to
> delete them.....
>
> Is there anyway to make a macro which would let me delete sheets.. based on
> their name.....
>
> all the ones i want to delete are called "cats (104)" or "cats
> (105)" etc etc..... there is one called "cats" which i want to keep.... so
> basically If it could someonie delete based on the term "cats ("
>
> any ideas?


--

Dave Peterson
 
Reply With Quote
 
Dnereb
Guest
Posts: n/a
 
      21st Aug 2005

The important thing to know is to itarate from top to bottom to avoi
problems due to the re-indexing of the worksheets by a workshee
delete

in other words if you delete worksheets(3)
worksheets(4) becomes worksheets(3)


Code
-------------------
Dim Ws As Worksheet
Dim Count As Long, L As Long


Count = ActiveWorkbook.Worksheets.Count

For Count = ActiveWorkbook.Worksheets.Count To 1 Step -1
Set Ws = ActiveWorkbook.Worksheets(Count)
If StrComp(Left$((Ws.Name), 3), "Cat", vbTextCompare) Then
Ws.Delete
End If
Nex
-------------------


I disn't test it but it should wor

--
Dnere
-----------------------------------------------------------------------
Dnereb's Profile: http://www.excelforum.com/member.php...fo&userid=2618
View this thread: http://www.excelforum.com/showthread.php?threadid=39756

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting sheets oldjay Microsoft Excel Programming 2 30th Nov 2009 07:14 PM
Re: Deleting Custom Views when Deleting Sheets NickHK Microsoft Excel Programming 0 28th Mar 2007 06:11 AM
populating sheets based on data from parent sheets seve Microsoft Excel Misc 2 15th Jan 2005 09:22 PM
Deleting Sheets based on checkboxes being unchecked =?Utf-8?B?VGlwIFRvcA==?= Microsoft Excel Programming 4 28th Jun 2004 02:53 PM
Re:Deleting sheets! TA Microsoft Excel Misc 2 24th Dec 2003 02:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:00 PM.