#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 50
Default macro

I have an Array("1","2","3") which correspond with the workbook sheet names.

how can I delete the sheets which is not in array list using VBA? I want
only to retain the sheets which is in the array list.

thanks
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default macro

Sub sheet_killer()
ssave = Array("1", "2", "3")
For Each sh In Sheets
n = sh.Name
killit = True
For i = 0 To 2
If n = ssave(i) Then
killit = False
End If
Next
If killit Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next
End Sub

So if you have sheets named:
Sheet1
Sheet2
Sheet5
1
2
3

then Sheet1, Sheet2, Sheet5 will be deleted.
--
Gary''s Student - gsnu200823


"EricBB" wrote:

I have an Array("1","2","3") which correspond with the workbook sheet names.

how can I delete the sheets which is not in array list using VBA? I want
only to retain the sheets which is in the array list.

thanks

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default macro

One mo

Option Explicit
Sub testme()
Dim myNames As Variant
Dim res As Variant
Dim sh As Object

myNames = Array("1", "2", "3")

For Each sh In ActiveWorkbook.Sheets
res = Application.Match(sh.Name, myNames, 0)
If IsError(res) Then
'not in array, so delete it
Application.DisplayAlerts = False
On Error Resume Next
sh.Delete
If Err.Number < 0 Then
Err.Clear
MsgBox sh.Name & " not deleted"
End If
On Error GoTo 0
Application.DisplayAlerts = True
End If
Next sh
End Sub




EricBB wrote:

I have an Array("1","2","3") which correspond with the workbook sheet names.

how can I delete the sheets which is not in array list using VBA? I want
only to retain the sheets which is in the array list.

thanks


--

Dave Peterson
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
need help to update macro to office 2007 macro enabled workbook jatman Excel Discussion (Misc queries) 1 December 14th 07 01:57 PM
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 3 February 5th 07 08:22 PM
using a cell value to control a counter inside a macro and displaying macro value ocset Excel Worksheet Functions 1 September 10th 06 05:32 AM
Macro needed to Paste Values and prevent Macro operation thunderfoot Excel Discussion (Misc queries) 0 June 10th 05 03:38 PM


All times are GMT +1. The time now is 10:35 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"