View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
keepITcool keepITcool is offline
external usenet poster
 
Posts: 2,253
Default efficiency qn: search collection or use on error?


a Scripting Dictionary is faster then a collection.

using a test on key (with on error) is MUCH faster then for each
(even with the dictionary which has a KeyExists method


For a collection following should work quite efficiently.

Function elementExists(myCollection As Collection, myKey As String) As
Boolean
On Error Resume Next
elementExists = (VarPtr(myCollection(myKey)) 0)
End Function



--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


noel mc wrote :

Hi,

Is it more efficient to explicitly search a collection for an entry
i.e. :

function elementExists(myCollection as collection, myName as string)
as boolean
elementExists = false
for each element in myCollection
if element.name = myName
elementExists = true
exit function
end if
next
end function

*OR* whether it is faster/better to use the name as a collection
parameter to call an item and catch any exceptions? (I'm not familiar
with the code used to implement the latter, but surely it must
perform the same kind of operation as the former...?)

Thanks all