Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 276
Default Delete all sheets after sheet1, but not by sheet name


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....


  #2   Report Post  
Posted to microsoft.public.excel.programming
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....



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Delete all sheets after sheet1, but not by sheet name

How bout something like:

Sub DeleteAllButFirst()
Dim i As Integer
Application.DisplayAlerts = False
For Each Sh In Worksheets
If Sh.Index 1 Then
Sh.Delete
End If
Next
Application.DisplayAlerts = True
End Sub

Regards,
Jim May


"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....


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 276
Default Delete all sheets after sheet1, but not by sheet name

Thanks Norman and Jim,
Too easy POST viewing the code isn't it.

Thanks Guys.

Corey....


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Delete all sheets after sheet1, but not by sheet name

Sub DeleteAllButFirst()
Dim i as Long
Application.DisplayAlerts = False
for i = 1 to Sheets.count - 1
Sheets(2).Delete
Next
Application.DisplayAlerts = True
End Sub

--
Regards,
Tom Ogilvy

"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....





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Delete all sheets after sheet1, but not by sheet name

Corey,

Dim i as Integer

For i = Sheets.Count to 1 Step -1 ' Step backwards since deleting will mess
up the total sheets
If Sheets(i).Name < "Sheet1" Then
Application.DisplayAlerts = False
Sheets(i).Delete
Applicaiton.DisplayAlerts = True
End If
Next i

Leave out the two lines for displaying alerts if you want the messagebox to
confirm the deletion.

David

"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....



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
Data in sheet1 to be put into sheets 2, 3, 4, 5, etc Cord Excel Discussion (Misc queries) 2 December 18th 09 10:19 AM
VBA Code Required for deleting All Sheets except Sheet1 Ms-Exl-Learner Excel Discussion (Misc queries) 7 November 3rd 09 10:19 AM
email sheets 2,3,4,5 ect. to users from a list on sheet1 swieduwi Excel Programming 7 June 13th 05 04:01 PM
How do I set up a workbook so changing Sheet1 changes all sheets? rach1027 Excel Worksheet Functions 1 June 7th 05 10:12 AM
copy sheet1 and name sheets using names from a range DL[_3_] Excel Programming 2 September 2nd 03 12:58 PM


All times are GMT +1. The time now is 04:53 AM.

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

About Us

"It's about Microsoft Excel"