<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Excluding Collection Members</title>
	<atom:link href="http://www.dailydoseofexcel.com/archives/2004/09/15/excluding-collection-members/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dailydoseofexcel.com/archives/2004/09/15/excluding-collection-members/</link>
	<description>Daily posts of Excel tips…and other stuff</description>
	<lastBuildDate>Thu, 09 Feb 2012 23:42:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Stan</title>
		<link>http://www.dailydoseofexcel.com/archives/2004/09/15/excluding-collection-members/#comment-2296</link>
		<dc:creator>Stan</dc:creator>
		<pubDate>Fri, 17 Sep 2004 20:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=764#comment-2296</guid>
		<description>&lt;p&gt;I&#039;d suggest setting up a dictionary object, like this:&lt;/p&gt;
&lt;p&gt;Dim d&lt;br&gt;
Sub CreateSheetDictionary()&lt;br&gt;
    Set d = CreateObject(&quot;Scripting.Dictionary&quot;)&lt;br&gt;
    d.Add &quot;sheet1?, &quot;1?&lt;br&gt;
    d.Add &quot;sheet4?, &quot;1?&lt;br&gt;
    d.Add &quot;sheet5?, &quot;1?&lt;br&gt;
End Sub&lt;/p&gt;
&lt;p&gt;Then, your delete routine looks like this:&lt;/p&gt;
&lt;p&gt;For Each ws In ThisWorkbook.Worksheets&lt;br&gt;
    if not(d.exists(ws.name)) then&lt;br&gt;
        ws.delete&lt;br&gt;
    end if&lt;br&gt;
Next&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I&#8217;d suggest setting up a dictionary object, like this:</p>
<p>Dim d<br />
Sub CreateSheetDictionary()<br />
    Set d = CreateObject(&#8220;Scripting.Dictionary&#8221;)<br />
    d.Add &#8220;sheet1?, &#8220;1?<br />
    d.Add &#8220;sheet4?, &#8220;1?<br />
    d.Add &#8220;sheet5?, &#8220;1?<br />
End Sub</p>
<p>Then, your delete routine looks like this:</p>
<p>For Each ws In ThisWorkbook.Worksheets<br />
    if not(d.exists(ws.name)) then<br />
        ws.delete<br />
    end if<br />
Next</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://www.dailydoseofexcel.com/archives/2004/09/15/excluding-collection-members/#comment-2295</link>
		<dc:creator>Kevin</dc:creator>
		<pubDate>Thu, 16 Sep 2004 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=764#comment-2295</guid>
		<description>&lt;p&gt;I like JT&#039;s example.  &lt;/p&gt;
&lt;p&gt;One additional piece of advice -- add these lines to avoid having to approve the deletion of every sheet:&lt;/p&gt;
&lt;p&gt;Application.DisplayAlerts = False&lt;br&gt;
Application.DisplayAlerts = True&lt;/p&gt;
&lt;p&gt;If you wrap them around ws.Delete then it won&#039;t prevent any other alerts from displaying.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I like JT&#8217;s example.  </p>
<p>One additional piece of advice &#8212; add these lines to avoid having to approve the deletion of every sheet:</p>
<p>Application.DisplayAlerts = False<br />
Application.DisplayAlerts = True</p>
<p>If you wrap them around ws.Delete then it won&#8217;t prevent any other alerts from displaying.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JT Heaverin</title>
		<link>http://www.dailydoseofexcel.com/archives/2004/09/15/excluding-collection-members/#comment-2294</link>
		<dc:creator>JT Heaverin</dc:creator>
		<pubDate>Thu, 16 Sep 2004 03:40:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=764#comment-2294</guid>
		<description>&lt;p&gt;It would be best to create a new collection of sheets to keep.&lt;br&gt;
This would eliminate the separation character and the test for the sheet.&lt;br&gt;
The only problem is the atrocious way VBA handles errors.&lt;/p&gt;
&lt;p&gt;Sub exclusion()&lt;br&gt;
    Dim Keep As String&lt;br&gt;
    Dim ws As Worksheet&lt;br&gt;
    Dim SheetsToKeep As Collection&lt;/p&gt;
&lt;p&gt;    &#039; Create a new collection&lt;br&gt;
    Set SheetsToKeep = New Collection&lt;/p&gt;
&lt;p&gt;    &#039;Add Names of sheets to keep&lt;br&gt;
    &#039;The &quot;&quot; can be anything,&lt;br&gt;
    &#039;it&#039;s the key we are interested in&lt;br&gt;
    SheetsToKeep.Add &quot;&quot;, &quot;Save1?&lt;br&gt;
    SheetsToKeep.Add &quot;&quot;, &quot;Save2?&lt;br&gt;
    SheetsToKeep.Add &quot;&quot;, &quot;Save3?&lt;/p&gt;
&lt;p&gt;    &#039;Loop through the sheets&lt;br&gt;
    On Error Resume Next&lt;br&gt;
    For Each ws In ThisWorkbook.Worksheets&lt;br&gt;
        Err.Clear&lt;br&gt;
        Keep = SheetsToKeep(ws.Name)&lt;br&gt;
        &#039;if the sheet is part of the collection&lt;br&gt;
        &#039;err is 0, otherwise err is not 0&lt;br&gt;
        If Err.Number &lt;&gt; 0 Then&lt;br&gt;
            ws.Delete&lt;br&gt;
        End If&lt;br&gt;
    Next ws&lt;br&gt;
    On Error GoTo 0&lt;br&gt;
End Sub&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>It would be best to create a new collection of sheets to keep.<br />
This would eliminate the separation character and the test for the sheet.<br />
The only problem is the atrocious way VBA handles errors.</p>
<p>Sub exclusion()<br />
    Dim Keep As String<br />
    Dim ws As Worksheet<br />
    Dim SheetsToKeep As Collection</p>
<p>    &#8216; Create a new collection<br />
    Set SheetsToKeep = New Collection</p>
<p>    &#8216;Add Names of sheets to keep<br />
    &#8216;The &#8220;&#8221; can be anything,<br />
    &#8216;it&#8217;s the key we are interested in<br />
    SheetsToKeep.Add &#8220;&#8221;, &#8220;Save1?<br />
    SheetsToKeep.Add &#8220;&#8221;, &#8220;Save2?<br />
    SheetsToKeep.Add &#8220;&#8221;, &#8220;Save3?</p>
<p>    &#8216;Loop through the sheets<br />
    On Error Resume Next<br />
    For Each ws In ThisWorkbook.Worksheets<br />
        Err.Clear<br />
        Keep = SheetsToKeep(ws.Name)<br />
        &#8216;if the sheet is part of the collection<br />
        &#8216;err is 0, otherwise err is not 0<br />
        If Err.Number &lt;&gt; 0 Then<br />
            ws.Delete<br />
        End If<br />
    Next ws<br />
    On Error GoTo 0<br />
End Sub</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Rynd</title>
		<link>http://www.dailydoseofexcel.com/archives/2004/09/15/excluding-collection-members/#comment-2293</link>
		<dc:creator>Jonathan Rynd</dc:creator>
		<pubDate>Thu, 16 Sep 2004 00:10:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=764#comment-2293</guid>
		<description>&lt;p&gt;Problem: What if one of the sheet names is a suffix of another sheet name?&lt;/p&gt;
&lt;p&gt;For instance, if you have MySheet1 and Sheet1 and you want to delete Sheet1.&lt;/p&gt;
&lt;p&gt;The code supplied would delete MySheet1 too.&lt;/p&gt;
&lt;p&gt;I&#039;d use this code instead:&lt;/p&gt;
&lt;p&gt;Sub exclusion()&lt;br&gt;
    Dim SheetsToKeep As String&lt;br&gt;
    Dim i As Long&lt;br&gt;
    Dim ws As Worksheet&lt;/p&gt;
&lt;p&gt;    &#039;Names of sheets to keep&lt;br&gt;
    &#039;note the first and last commas&lt;br&gt;
    SheetsToKeep = &quot;,Save1,Save2,Save3,&quot;&lt;/p&gt;
&lt;p&gt;    &#039;Loop through the sheets&lt;br&gt;
    For Each ws In ThisWorkbook.Worksheets&lt;br&gt;
        &#039;See if the sheet&#039;s name is in the string&lt;br&gt;
        &#039;don&#039;t forget the commas&lt;br&gt;
        If InStr(1, SheetsToKeep, &quot;,&quot; &amp; ws.Name &amp; &quot;,&quot;) = 0 Then&lt;br&gt;
            ws.Delete&lt;br&gt;
        End If&lt;br&gt;
    Next ws&lt;/p&gt;
&lt;p&gt;End Sub&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Problem: What if one of the sheet names is a suffix of another sheet name?</p>
<p>For instance, if you have MySheet1 and Sheet1 and you want to delete Sheet1.</p>
<p>The code supplied would delete MySheet1 too.</p>
<p>I&#8217;d use this code instead:</p>
<p>Sub exclusion()<br />
    Dim SheetsToKeep As String<br />
    Dim i As Long<br />
    Dim ws As Worksheet</p>
<p>    &#8216;Names of sheets to keep<br />
    &#8216;note the first and last commas<br />
    SheetsToKeep = &#8220;,Save1,Save2,Save3,&#8221;</p>
<p>    &#8216;Loop through the sheets<br />
    For Each ws In ThisWorkbook.Worksheets<br />
        &#8216;See if the sheet&#8217;s name is in the string<br />
        &#8216;don&#8217;t forget the commas<br />
        If InStr(1, SheetsToKeep, &#8220;,&#8221; &amp; ws.Name &amp; &#8220;,&#8221;) = 0 Then<br />
            ws.Delete<br />
        End If<br />
    Next ws</p>
<p>End Sub</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob van Gelder</title>
		<link>http://www.dailydoseofexcel.com/archives/2004/09/15/excluding-collection-members/#comment-2292</link>
		<dc:creator>Rob van Gelder</dc:creator>
		<pubDate>Wed, 15 Sep 2004 22:56:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=764#comment-2292</guid>
		<description>&lt;p&gt;I would take your second approach. If you didn&#039;t want to repeat the exclusion check code every time, make a function to return true/false.&lt;br&gt;
eg.&lt;br&gt;
For Each ws In ThisWorkbook.Worksheets&lt;br&gt;
    If Not ExclCheck(ws.Name) Then ws.Delete&lt;br&gt;
Next ws&lt;/p&gt;
&lt;p&gt;I try to use a separator character which is likely not be used for sheet names.&lt;br&gt;
Instead of comma, You could use section-break ?&lt;br&gt;
It&#039;s easy to type it ALT, numkey2, numkey1&lt;/p&gt;
&lt;p&gt;Then again, using an exclusion check function you would probably use an array rather than CSV&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I would take your second approach. If you didn&#8217;t want to repeat the exclusion check code every time, make a function to return true/false.<br />
eg.<br />
For Each ws In ThisWorkbook.Worksheets<br />
    If Not ExclCheck(ws.Name) Then ws.Delete<br />
Next ws</p>
<p>I try to use a separator character which is likely not be used for sheet names.<br />
Instead of comma, You could use section-break ?<br />
It&#8217;s easy to type it ALT, numkey2, numkey1</p>
<p>Then again, using an exclusion check function you would probably use an array rather than CSV</p>
<p>Cheers!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

