<?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: IsIn Collection or Array</title>
	<atom:link href="http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/</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: Rick Rothstein (MVP - Excel)</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-52892</link>
		<dc:creator>Rick Rothstein (MVP - Excel)</dc:creator>
		<pubDate>Wed, 20 Oct 2010 04:24:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-52892</guid>
		<description>&lt;p&gt;@Jocelyn,&lt;/p&gt;
&lt;p&gt;I cannot think of any other errors of consequence that this code could possibly raise, so On Error Resume Next should be fine to use as shown. Perhaps to be safe, though, we might add one more line to protect any other error handlers that might exist &quot;up the line&quot;...&lt;/p&gt;
&lt;p&gt;Err.Clear&lt;/p&gt;
&lt;p&gt;Make this the last line of code in the function. Without it, the Err object would hold onto any generated error which, if one were not careful, could trip up error trapping code in the calling routine (not very likely, but possible, so we might as well protect against it). We do **not** need to execute an On Error GoTo 0 statement to close the error handler as it will die automatically when the function ends.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@Jocelyn,</p>
<p>I cannot think of any other errors of consequence that this code could possibly raise, so On Error Resume Next should be fine to use as shown. Perhaps to be safe, though, we might add one more line to protect any other error handlers that might exist &#8220;up the line&#8221;&#8230;</p>
<p>Err.Clear</p>
<p>Make this the last line of code in the function. Without it, the Err object would hold onto any generated error which, if one were not careful, could trip up error trapping code in the calling routine (not very likely, but possible, so we might as well protect against it). We do **not** need to execute an On Error GoTo 0 statement to close the error handler as it will die automatically when the function ends.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fzz</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-52889</link>
		<dc:creator>fzz</dc:creator>
		<pubDate>Wed, 20 Oct 2010 04:15:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-52889</guid>
		<description>&lt;p&gt;I guess there are differences of opinion about passing objects as generic Object types or a specific object types since VBA doesn&#039;t syntax check different object types. So it&#039;s a question of where and how to throw the error. If errors must be thrown, then I&#039;d declare procedure arguments as specific object types and let VBA throw the runtime error. If wrong object type errors should always be caught, then better to do so at the source rather than in a library procedure.&lt;/p&gt;
&lt;p&gt;So maybe something like&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; f(c &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Object&lt;/span&gt;, k &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Boolean&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt; c &lt;span class=&quot;kw1&quot;&gt;Is&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Then&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;co1&quot;&gt;&#039;nothing to do, will return False&lt;br&gt;
&lt;/span&gt; &#160;&lt;span class=&quot;kw1&quot;&gt;ElseIf&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;TypeOf&lt;/span&gt; c &lt;span class=&quot;kw1&quot;&gt;Is&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Then&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;On&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Resume&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&#160; &#160; f = VarType(c(k)) + 1&lt;br&gt;
&#160; &#160; Err.Clear&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;Else&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;co1&quot;&gt;&#039;1st arg invalid, take whatever action you want&lt;br&gt;
&lt;/span&gt; &#160;&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If c(k) doesn&#039;t throw an error, VarType will ALWAYS return an integer, and the integers it returns will NEVER throw overflow/underflow errors when incremented by 1. So only 2 errors could occur: k isn&#039;t a key in c; c is neither a Collection object or Nothing. [Note: Nothing is treated as a Collection with no items/keys.] Trapping all other errors is madness. The only other runtime errors which might occur are environmental ones like no more stack space, out of memory, CPU overheating, etc. Nothing gained trying to trap those.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I guess there are differences of opinion about passing objects as generic Object types or a specific object types since VBA doesn&#8217;t syntax check different object types. So it&#8217;s a question of where and how to throw the error. If errors must be thrown, then I&#8217;d declare procedure arguments as specific object types and let VBA throw the runtime error. If wrong object type errors should always be caught, then better to do so at the source rather than in a library procedure.</p>
<p>So maybe something like</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Function</span> f(c <span class="kw1">As</span> <span class="kw1">Object</span>, k <span class="kw1">As</span> <span class="kw1">String</span>) <span class="kw1">As</span> <span class="kw1">Boolean</span><br />
&nbsp; <span class="kw1">If</span> c <span class="kw1">Is</span> <span class="kw1">Nothing</span> <span class="kw1">Then</span><br />
&nbsp; &nbsp; <span class="co1">&#8216;nothing to do, will return False<br />
</span> &nbsp;<span class="kw1">ElseIf</span> <span class="kw1">TypeOf</span> c <span class="kw1">Is</span> <span class="kw1">Collection</span> <span class="kw1">Then</span><br />
&nbsp; &nbsp; <span class="kw1">On</span> <span class="kw1">Error</span> <span class="kw1">Resume</span> <span class="kw1">Next</span><br />
&nbsp; &nbsp; f = VarType(c(k)) + 1<br />
&nbsp; &nbsp; Err.Clear<br />
&nbsp; <span class="kw1">Else</span><br />
&nbsp; &nbsp; <span class="co1">&#8217;1st arg invalid, take whatever action you want<br />
</span> &nbsp;<span class="kw1">End</span> <span class="kw1">If</span><br />
<span class="kw1">End</span> <span class="kw1">Function</span></div>
</div>
<p>If c(k) doesn&#8217;t throw an error, VarType will ALWAYS return an integer, and the integers it returns will NEVER throw overflow/underflow errors when incremented by 1. So only 2 errors could occur: k isn&#8217;t a key in c; c is neither a Collection object or Nothing. [Note: Nothing is treated as a Collection with no items/keys.] Trapping all other errors is madness. The only other runtime errors which might occur are environmental ones like no more stack space, out of memory, CPU overheating, etc. Nothing gained trying to trap those.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jocelyn Paine</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-52881</link>
		<dc:creator>Jocelyn Paine</dc:creator>
		<pubDate>Wed, 20 Oct 2010 01:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-52881</guid>
		<description>&lt;p&gt;Rick and fzz, I&#039;m still worried about what Lon says about errors: &quot;They [the functions above his postings] also hide any other error that might rear its head. This would not be a good idea in production code. Why do the On Error Resume Next nonsense when you know what the error will be?&quot;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Rick and fzz, I&#8217;m still worried about what Lon says about errors: &#8220;They [the functions above his postings] also hide any other error that might rear its head. This would not be a good idea in production code. Why do the On Error Resume Next nonsense when you know what the error will be?&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Rothstein (MVP - Excel)</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-52875</link>
		<dc:creator>Rick Rothstein (MVP - Excel)</dc:creator>
		<pubDate>Tue, 19 Oct 2010 22:10:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-52875</guid>
		<description>&lt;p&gt;@fzz,&lt;/p&gt;
&lt;p&gt;Good point! I guess we can extend your idea to one of these assignments as well...&lt;/p&gt;
&lt;p&gt;f = VarType(c(k)) &gt;= 0&lt;/p&gt;
&lt;p&gt;or...&lt;/p&gt;
&lt;p&gt;f = IsNumeric(VarType(c(k))&lt;/p&gt;
&lt;p&gt;or...&lt;/p&gt;
&lt;p&gt;f = VarType(C(k)) Like &quot;#&quot;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@fzz,</p>
<p>Good point! I guess we can extend your idea to one of these assignments as well&#8230;</p>
<p>f = VarType(c(k)) &gt;= 0</p>
<p>or&#8230;</p>
<p>f = IsNumeric(VarType(c(k))</p>
<p>or&#8230;</p>
<p>f = VarType(C(k)) Like &#8220;#&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fzz</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-52873</link>
		<dc:creator>fzz</dc:creator>
		<pubDate>Tue, 19 Oct 2010 21:06:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-52873</guid>
		<description>&lt;p&gt;Why bother with the Len call?&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; f(c &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;, k &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Boolean&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;On&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Resume&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&#160; f = VarType(c(k)) + 1&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
</description>
		<content:encoded><![CDATA[<p>Why bother with the Len call?</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Function</span> f(c <span class="kw1">As</span> <span class="kw1">Collection</span>, k <span class="kw1">As</span> <span class="kw1">String</span>) <span class="kw1">As</span> <span class="kw1">Boolean</span><br />
&nbsp; <span class="kw1">On</span> <span class="kw1">Error</span> <span class="kw1">Resume</span> <span class="kw1">Next</span><br />
&nbsp; f = VarType(c(k)) + 1<br />
<span class="kw1">End</span> <span class="kw1">Function</span></div>
</div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Rothstein (MVP - Excel)</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-52838</link>
		<dc:creator>Rick Rothstein (MVP - Excel)</dc:creator>
		<pubDate>Tue, 19 Oct 2010 04:09:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-52838</guid>
		<description>&lt;p&gt;Just to note that since IsIn2 is declared as a Boolean, then Stephan Bullen&#039;s code should be able to be simplified (ever so slightly) to this...&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container vb default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;vb codecolorer&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; IsIn2(oCollection &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Object&lt;/span&gt;, sKey &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Boolean&lt;/span&gt;&lt;br&gt;
&#160; &lt;span class=&quot;kw1&quot;&gt;On&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Resume&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&#160; IsIn2 = Len(TypeName(oCollection(sKey)))&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
</description>
		<content:encoded><![CDATA[<p>Just to note that since IsIn2 is declared as a Boolean, then Stephan Bullen&#8217;s code should be able to be simplified (ever so slightly) to this&#8230;</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container vb default">
<div style="white-space: nowrap;" class="vb codecolorer"><span class="kw1">Function</span> IsIn2(oCollection <span class="kw1">As</span> <span class="kw1">Object</span>, sKey <span class="kw1">As</span> <span class="kw1">String</span>) <span class="kw1">As</span> <span class="kw1">Boolean</span><br />
&nbsp; <span class="kw1">On</span> <span class="kw1">Error</span> <span class="kw1">Resume</span> <span class="kw1">Next</span><br />
&nbsp; IsIn2 = Len(TypeName(oCollection(sKey)))<br />
<span class="kw1">End</span> <span class="kw1">Function</span></div>
</div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jocelyn Paine</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-52837</link>
		<dc:creator>Jocelyn Paine</dc:creator>
		<pubDate>Tue, 19 Oct 2010 03:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-52837</guid>
		<description>&lt;p&gt;This discussion is very useful. I&#039;d been using Chip Pearson&#039;s KeyExistsInCollection, from &lt;a href=&quot;http://www.cpearson.com/excel/CollectionsAndDictionaries.htm&quot; rel=&quot;nofollow&quot;&gt;http://www.cpearson.com/excel/CollectionsAndDictionaries.htm&lt;/a&gt; :&lt;br&gt;
&lt;code class=&quot;codecolorer vb default&quot;&gt;&lt;span class=&quot;vb&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; KeyExistsInCollection(Coll &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;, KeyName &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Boolean&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; V &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Variant&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;On&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Resume&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;
&#160; &#160; Err.Clear&lt;br&gt;
&#160; &#160; V = Coll(KeyName)&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt; Err.Number = 0 &lt;span class=&quot;kw1&quot;&gt;Then&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160; &#160; KeyExistsInCollection = &lt;span class=&quot;kw1&quot;&gt;True&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;Else&lt;/span&gt;&lt;br&gt;
&#160; &#160; &#160; &#160; KeyExistsInCollection = &lt;span class=&quot;kw1&quot;&gt;False&lt;/span&gt;&lt;br&gt;
&#160; &#160; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This worked when my collections contained strings. But I&#039;ve just spent half an hour tracking down a bug caused by it not working for a collection that contains collections. Presumably because then one needs a&lt;/p&gt;
&lt;div style=&quot;overflow: auto; white-space: nowrap;&quot; class=&quot;codecolorer-container text default&quot;&gt;&lt;div style=&quot;white-space: nowrap;&quot; class=&quot;text codecolorer&quot;&gt;Set V = Coll(KeyName)&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;. I note Lon Ingram&#039;s warning about hiding other errors that might raise their heads. But I also like Stephen Bullen&#039;s code, because it&#039;s short and very easy to understand. Lon, or anyone else, what other errors could arise other than the two you make constants for?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This discussion is very useful. I&#8217;d been using Chip Pearson&#8217;s KeyExistsInCollection, from <a href="http://www.cpearson.com/excel/CollectionsAndDictionaries.htm" rel="nofollow">http://www.cpearson.com/excel/CollectionsAndDictionaries.htm</a> :</p>
<div class="codecolorer-container text default codecolorer vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;span class=&quot;vb&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;Public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt; KeyExistsInCollection(Coll &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Collection&lt;/span&gt;, KeyName &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Boolean&lt;/span&gt;&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw1&quot;&gt;Dim&lt;/span&gt; V &lt;span class=&quot;kw1&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Variant&lt;/span&gt;&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw1&quot;&gt;On&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Resume&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Next&lt;/span&gt;&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; Err.Clear&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; V = Coll(KeyName)&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt; Err.Number = 0 &lt;span class=&quot;kw1&quot;&gt;Then&lt;/span&gt;&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; KeyExistsInCollection = &lt;span class=&quot;kw1&quot;&gt;True&lt;/span&gt;&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw1&quot;&gt;Else&lt;/span&gt;&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; KeyExistsInCollection = &lt;span class=&quot;kw1&quot;&gt;False&lt;/span&gt;&lt;br&gt;<br />
&amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;If&lt;/span&gt;&lt;br&gt;<br />
&lt;span class=&quot;kw1&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;Function&lt;/span&gt;&lt;/span&gt;</div></div>
</p>
<p>This worked when my collections contained strings. But I&#8217;ve just spent half an hour tracking down a bug caused by it not working for a collection that contains collections. Presumably because then one needs a</p>
<div style="overflow: auto; white-space: nowrap;" class="codecolorer-container text default">
<div style="white-space: nowrap;" class="text codecolorer">Set V = Coll(KeyName)</div>
</div>
<p>. I note Lon Ingram&#8217;s warning about hiding other errors that might raise their heads. But I also like Stephen Bullen&#8217;s code, because it&#8217;s short and very easy to understand. Lon, or anyone else, what other errors could arise other than the two you make constants for?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lon Ingram</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-13718</link>
		<dc:creator>Lon Ingram</dc:creator>
		<pubDate>Mon, 13 Jun 2005 10:26:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-13718</guid>
		<description>&lt;p&gt;Whoops, above should be:&lt;/p&gt;
&lt;p&gt;Private Const ERR_BAD_ARGUMENT As Long = 5&lt;br&gt;
Private Const ERR_BAD_SUBSCRIPT As Long = 9&lt;/p&gt;
&lt;p&gt;Public Function IsInCol(oCol As Object, sKey As String)&lt;br&gt;
Dim f As Boolean&lt;/p&gt;
&lt;p&gt;On Error GoTo ErrWrangler&lt;/p&gt;
&lt;p&gt;f = IsObject(oCol.Item(sKey))&lt;br&gt;
IsInCol = True&lt;/p&gt;
&lt;p&gt;ExitHere:&lt;br&gt;
Exit Function&lt;/p&gt;
&lt;p&gt;ErrWrangler:&lt;br&gt;
Select Case Err.Number&lt;br&gt;
Case ERR_BAD_ARGUMENT, ERR_BAD_SUBSCRIPT&lt;br&gt;
IsInCol = False&lt;br&gt;
Err.Clear&lt;br&gt;
Resume ExitHere&lt;/p&gt;
&lt;p&gt;Case Else&lt;br&gt;
Err.Raise Err.Number &#039;yada yada&lt;br&gt;
End Select&lt;br&gt;
End Function&lt;/p&gt;
&lt;p&gt;No variant, too.  Woot!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Whoops, above should be:</p>
<p>Private Const ERR_BAD_ARGUMENT As Long = 5<br />
Private Const ERR_BAD_SUBSCRIPT As Long = 9</p>
<p>Public Function IsInCol(oCol As Object, sKey As String)<br />
Dim f As Boolean</p>
<p>On Error GoTo ErrWrangler</p>
<p>f = IsObject(oCol.Item(sKey))<br />
IsInCol = True</p>
<p>ExitHere:<br />
Exit Function</p>
<p>ErrWrangler:<br />
Select Case Err.Number<br />
Case ERR_BAD_ARGUMENT, ERR_BAD_SUBSCRIPT<br />
IsInCol = False<br />
Err.Clear<br />
Resume ExitHere</p>
<p>Case Else<br />
Err.Raise Err.Number &#8216;yada yada<br />
End Select<br />
End Function</p>
<p>No variant, too.  Woot!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lon Ingram</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-13717</link>
		<dc:creator>Lon Ingram</dc:creator>
		<pubDate>Mon, 13 Jun 2005 10:01:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-13717</guid>
		<description>&lt;p&gt;RobertV:&lt;br&gt;
1. Explicitly destroying objects is deprecated.  Your O will be destroyed when it goes out of scope.  I used to do this too, because the MS Press books do it.  Then I read about it on Eric Lippert&#039;s blog.&lt;/p&gt;
&lt;p&gt;The functions listed all assume that the passed collection is not nothing.  They also hide any other error that might rear its head.  This would not be a good idea in production code.  Why do the On Error Resume Next nonsense when you know what the error will be?  &lt;/p&gt;
&lt;p&gt;I would do something more along the lines of:&lt;/p&gt;
&lt;p&gt;Private Const ERR_BAD_ARGUMENT As Long = 5&lt;br&gt;
Private Const ERR_BAD_SUBSCRIPT As Long = 9&lt;/p&gt;
&lt;p&gt;Public Function IsInCol(oCol As Object, sKey As String)&lt;br&gt;
Dim v As Variant&lt;/p&gt;
&lt;p&gt;On Error GoTo ErrWrangler&lt;/p&gt;
&lt;p&gt;v = oCol.Item(sKey)&lt;/p&gt;
&lt;p&gt;IsInCol = True&lt;/p&gt;
&lt;p&gt;ExitHere:&lt;br&gt;
Exit Function&lt;/p&gt;
&lt;p&gt;ErrWrangler:&lt;br&gt;
Select Case Err.Number&lt;br&gt;
Case ERR_BAD_ARGUMENT, ERR_BAD_SUBSCRIPT&lt;br&gt;
IsInCol = False&lt;br&gt;
Err.Clear&lt;br&gt;
Resume ExitHere&lt;/p&gt;
&lt;p&gt;Case Else&lt;br&gt;
Err.Raise Err.Number &#039;yada yada&lt;br&gt;
End Select&lt;br&gt;
End Function&lt;/p&gt;
&lt;p&gt;I didn&#039;t do much testing on this, so use at your own risk.  Should be safer than the other versions posted here.  Yes, variants are slow, but so are collections.  Code a custom type-specific version if you really need it.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>RobertV:<br />
1. Explicitly destroying objects is deprecated.  Your O will be destroyed when it goes out of scope.  I used to do this too, because the MS Press books do it.  Then I read about it on Eric Lippert&#8217;s blog.</p>
<p>The functions listed all assume that the passed collection is not nothing.  They also hide any other error that might rear its head.  This would not be a good idea in production code.  Why do the On Error Resume Next nonsense when you know what the error will be?  </p>
<p>I would do something more along the lines of:</p>
<p>Private Const ERR_BAD_ARGUMENT As Long = 5<br />
Private Const ERR_BAD_SUBSCRIPT As Long = 9</p>
<p>Public Function IsInCol(oCol As Object, sKey As String)<br />
Dim v As Variant</p>
<p>On Error GoTo ErrWrangler</p>
<p>v = oCol.Item(sKey)</p>
<p>IsInCol = True</p>
<p>ExitHere:<br />
Exit Function</p>
<p>ErrWrangler:<br />
Select Case Err.Number<br />
Case ERR_BAD_ARGUMENT, ERR_BAD_SUBSCRIPT<br />
IsInCol = False<br />
Err.Clear<br />
Resume ExitHere</p>
<p>Case Else<br />
Err.Raise Err.Number &#8216;yada yada<br />
End Select<br />
End Function</p>
<p>I didn&#8217;t do much testing on this, so use at your own risk.  Should be safer than the other versions posted here.  Yes, variants are slow, but so are collections.  Code a custom type-specific version if you really need it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RobertV</title>
		<link>http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/#comment-13712</link>
		<dc:creator>RobertV</dc:creator>
		<pubDate>Sun, 12 Jun 2005 08:54:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1159#comment-13712</guid>
		<description>&lt;p&gt;1. For completeness sake, shouldn&#039;t a created object always be annihilated when no longer in use?&lt;/p&gt;
&lt;p&gt;Function IsIn(oCollection As Object, stName As String) As Boolean&lt;br&gt;
    Dim O As Object&lt;br&gt;
    On Error GoTo NotIn&lt;br&gt;
    Set O = oCollection(stName)&lt;br&gt;
    IsIn = True&lt;br&gt;
    Set O = Nothing&lt;/p&gt;
&lt;p&gt;NotIn:&lt;br&gt;
End Function&lt;/p&gt;
&lt;p&gt;2. Dictionary Objects do have a builtin method Exists to check whether or not a member exist.  This bypasses the ugly ON Error statement. I understand that the use of some Collections (e.g WorkSheets, Charts, ...) is imposed by the Excel Object Model but I always wonder why Dictionary Objects (which are much faster and more natural) are not more widely used.  Perhaps because you have to include them through a reference to vbscript? (vbscripting runtime?).  Any opinions are wellcomed.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>1. For completeness sake, shouldn&#8217;t a created object always be annihilated when no longer in use?</p>
<p>Function IsIn(oCollection As Object, stName As String) As Boolean<br />
    Dim O As Object<br />
    On Error GoTo NotIn<br />
    Set O = oCollection(stName)<br />
    IsIn = True<br />
    Set O = Nothing</p>
<p>NotIn:<br />
End Function</p>
<p>2. Dictionary Objects do have a builtin method Exists to check whether or not a member exist.  This bypasses the ugly ON Error statement. I understand that the use of some Collections (e.g WorkSheets, Charts, &#8230;) is imposed by the Excel Object Model but I always wonder why Dictionary Objects (which are much faster and more natural) are not more widely used.  Perhaps because you have to include them through a reference to vbscript? (vbscripting runtime?).  Any opinions are wellcomed.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

