Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Marco to Delete Worksheets

I want to create a macro the deletes all worksheets within my workbook except
5. The worksheets I don't want deleted are named: Template, TY, LY, 52WkEnd,
and Macro. What code should I used to achieve this?
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default Marco to Delete Worksheets

Hi,

ALT+F11 to open VB editor, right click 'ThisWorkbook' and insert module and
paste the code below in and run it

Sub selete_Sheets()
Dim ws As Worksheet
Dim DelFlag As Boolean
Dim MyArray As Variant
MyArray = Array("Template", "TY", "LY", "52WkEnd", "Macro")
For Each ws In ThisWorkbook.Worksheets
DelFlag = True
For x = LBound(MyArray) To UBound(MyArray)
If ws.Name = MyArray(x) Then
DelFlag = False
Exit For
End If
Next
If DelFlag Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
Next
End Sub

Mike

"CM4@FL" wrote:

I want to create a macro the deletes all worksheets within my workbook except
5. The worksheets I don't want deleted are named: Template, TY, LY, 52WkEnd,
and Macro. What code should I used to achieve this?

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,316
Default Marco to Delete Worksheets

Press Alt + F11 to open the Visual Basic Editor and click INSERT on the menu
and select MODULE. You can paste the code below in to your module.

Sub RemoveSheets()

Dim wb As Workbook
Dim ws As Worksheet
Dim strWSName As String

Set wb = ThisWorkbook
'Suppress alerts and screen updates
With Application
.DisplayAlerts = False
.StatusBar = "Removing unwanted worksheets..."
.ScreenUpdating = False
End With
'Cycle through all sheets and remove all but the
'named sheets below
For Each ws In wb.Worksheets
strWSName = ws.Name
Select Case strWSName
Case "Template", "TY", "LY", "52WkEnd", "Macro"
Case Else
ws.Delete
End Select
Next ws
'Reset application screen and alerts settings
With Application
.DisplayAlerts = True
.StatusBar = False
.ScreenUpdating = True
End With

Set ws = Nothing
Set wb = Nothing

End Sub
--
Kevin Backmann


"CM4@FL" wrote:

I want to create a macro the deletes all worksheets within my workbook except
5. The worksheets I don't want deleted are named: Template, TY, LY, 52WkEnd,
and Macro. What code should I used to achieve this?

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
How to delete blank worksheets Keep_It_Simple_Stupid Excel Worksheet Functions 1 January 23rd 08 09:35 AM
Delete all rows below certain value on multiple worksheets? [email protected] Excel Discussion (Misc queries) 1 January 4th 08 05:51 PM
Delete All Worksheets Apart From Some With Particular Name Dave Excel Discussion (Misc queries) 1 October 31st 07 01:30 AM
I can't delete my worksheets Dillon Excel Discussion (Misc queries) 1 March 5th 07 06:10 PM
marco to delete rows Johnfli Excel Discussion (Misc queries) 2 January 11th 07 04:50 PM


All times are GMT +1. The time now is 10:31 PM.

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"