View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Barb Reinhardt Barb Reinhardt is offline
external usenet poster
 
Posts: 3,355
Default Delete Worksheets Named Sheet1, Sheet2, Sheet3, etc.

First, you have to declare ws (or aws) as a worksheet
And you need
For each aws in Activeworkbook.worksheets

I don't think you had that in some.

"ryguy7272" wrote:

Excellent!! That's exactly it!! How come my version did not work Bara? Did
it have something to do with the ".Name Like "Sheet*" Then"?

--
RyGuy


"Barb Reinhardt" wrote:

Sub DeleteSheets()
Dim aWS As Worksheet

For Each aWS In ActiveWorkbook.Worksheets
Debug.Print aWS.Name
If aWS.Name Like "Sheet*" Then
aWS.Delete
End If
Next aWS

End Sub
"ryguy7272" wrote:

Some great people helped me out with code similar to that posted below. I
used a slightly modified version of this code to delete a few specific
sheets, and I am now trying to modify it to delete any sheet named Sheet1,
Sheet2, Sheet3, etc. Basically, I have code that loops and builds a workbook
with many sheets that contain information about stocks. Sometimes the loop
spits out sheets named Sheet1, Sheet2, etc. and then it ends. Im trying to
find a way to delete these few sheets that are created. I just cant figure
out why the samples of code below will not delete the superfluous sheets.
Does VBA support the * character?


Sub DelSheets()

For Each ws In Worksheets
If ws.Name = "Sheet*" Then ws.Delete
Next ws

For Each ws In Workbooks
If ws.Name Like "Sheet*" Then ws.Delete
Next ws

For Each sh In ActiveWorkbook.Worksheets
If InStr(1, sh.Name, "Sheet*") Then
sh.Delete
End If
Next sh

End Sub

--
RyGuy