How does Collection.Item search

G

Guest

I recently moved from searching for data by arrays to collections, but found
that my macros now take 2 or 3 minute to process whereas when I was using
arrays it took 2 or 3 seconds to do the same work. But I don't want to go
back to arrays because indexing is always alway error prone.

I could add a binary search subroutine to speed up the search, but I was
wondering if that's already included in the Collections class. So, how does
Collections use the key to look up an item? Is it a simple sequential
search? If the adds are made with the keys presented alphabetically (less
work than debugging a binary search subroutine), will it respond better?
Thanks.
 
G

Guest

I suspect it builds a hash table or index

This impression is reinforced from an Article on MSDN about working with
collections:

"This is because after deleting the first item, all items in the collection
are re-indexed so that what was the second item is now the first. "

A dictionary object from the scripting runtime may give you more of what you
need.
 
G

Guest

I saw the docs on the dictionary and tried it, specifically (copying from the
help file) :

Sub testDictionary()
Dim d 'Create a variable
Set d = CreateObject(Scripting.Dictionary)
d.add "a", "Athens" 'Add some keys and items
d.add "b", "Belgrade"
d.add "c", "Cairo"
End Sub

But when I try to execute (F8) it, I get "Compile Error: Variable not
Defined" and the word "Scripting" is not highlighted.

Suggestions?
 
G

Guest

Er....Make that "Scripting" is highlighted.

Tom Ogilvy said:
I suspect it builds a hash table or index

This impression is reinforced from an Article on MSDN about working with
collections:

"This is because after deleting the first item, all items in the collection
are re-indexed so that what was the second item is now the first. "

A dictionary object from the scripting runtime may give you more of what you
need.
 
D

Dave Peterson

Set D = CreateObject("Scripting.Dictionary")

It's just a string here, so surround it with double quotes.
 
D

Dana DeLouis

Set d = CreateObject(Scripting.Dictionary)


Just to let you know, that error in the Help system has been there for a
long time.

Set d = CreateObject("Scripting.Dictionary")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top