Jim Cone explained on 1/18/2012 :
Chip Pearson has some comments on "Don't Use Auto-Instancing Object
Variables" at
http://www.cpearson.com/excel/DeclaringVariables.aspx
A Collection object uses the Key to make its decisions. The Item is just
along for the ride.
You can stick almost anything into the Item.
Thanks, Jim.
I looked at Chip's article and I agree with it without reservation. I
reiterate, though, my comments regarding non-intrinsic objects since
his comments ref the external Scripting lib. Nonetheless, since we do
not work with the Collection object in the ways he points out, my
position still stands for using auto-instancing because the object
doesn't get instantiated until needed in the function and doesn't
persist to exist outside the function. I guess it's a matter of knowing
when it's okay to use auto-instancing and when not to use it. I suppose
it's also good practice to be consistent in how one handles this,
though in my world there's room for variable consistency<g> when
warranted. In the context of this function the Collection is also
auto-destroyed and so no need to add extra Set statements<IMO>. I'm a
strong supporter of explicitly destroying any objects we explicitly
create, and so...
Dim cRngB As Collection
Set cRngB = New Collection
...should be explicitly destroyed as a point of 'good practice' when
we're done with it...
Set cRngB = Nothing
...so VBA doesn't have to do the extra processing involved with implicit
destruction of the object variable. Not a big deal on a one-by-one
basis but the performance effect can be accumulative over the life of a
project's runtime.
So the variable consistency I use is...
Create explicitly; destroy explicitly
Auto-instance; auto-destroy
--
I'm okay with the way the Key/Item is handled since both are required
inputs and so must be some value when added. I don't see any advantage
in swapping the same value as Key with vbNullString for Item. I could,
however, swap using Item for the check to using Key since Key should be
unique while Item can be anything. I realize we use the same value for
both but the logical test in the real world will 'usually' be made on
Key if known, Item otherwise. Ron's implementation is good either way
IMO.