View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones Norman Jones is offline
external usenet poster
 
Posts: 5,302
Default Delete all sheets after sheet1, but not by sheet name

Hi Corey,

To delate all sheers except for the first sheet, try:

'=============
Public Sub Tester001()
Dim WB As Workbook
Dim SH As Object
Dim i As Long

Set WB = Workbooks("YourBook.xls")

With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With

For i = WB.Sheets.Count To 2 Step -1
WB.Sheets(i).Delete
Next i

With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
'<<=============

To deleta all sheets except for a sheet named Sheet1, try instead:

'=============
Public Sub Tester002()
Dim WB As Workbook
Dim SH As Object
Const ShName As String = "Sheet1" '<<==== CHANGE

Set WB = Workbooks("YourBook.xls")

With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With

For Each SH In WB.Sheets
If SH.Name < ShName Then
SH.Delete
End If
Next SH

With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
'<<=============


---
Regards,
Norman



"Corey" wrote in message
...

is there some code where i can delete all sheets, except for sheet1, but
not by the sheet name.
As the sheet names are not in any particular sequence or indexed value?
Corey....