tags:

views:

7509

answers:

3

Hi.

Does VBA have dictionary structure? Like key<>value array?

+5  A: 

Yes. For VB6 (and VB.NET)

Matthew Flaschen
not sure what VB.NET has to do with VBA?
Mitch Wheat
You can read question ones more: I've asked about VBA: Visual Basic for Application, not for VB, not for VB.Net, not for any other language.
fessGUID: then again, you should read answers more! This answer can also be used for VBA (in particular, the first link).
Konrad Rudolph
which is why I asked the original question about relevance of VB.NET...
Mitch Wheat
I admit. I read the question too fast. But I did tell him what he needed to know.
Matthew Flaschen
The relevance of .Net is that sooner or later VBA (which is currently a subset of VB6) will end up moving to being a subset of .Net. Besides you should never complain if people give you more than you ask for. It's just ungrateful.
Oorang
@Oorang, there's absolutely no evidence of VBA becoming a subset of VB.NET, backcompat rules in Office - imagine trying to convert every Excel macro ever written.
Richard Gadsden
+14  A: 

Yes.

Set a reference to MS Scripting runtime

Set dict = CreateObject("Scripting.Dictionary")

or

Dim dict As New Scripting.Dictionary

Example of use:

If Not dict.Exists(key) Then 
    dict.Add key, value
End If

Don't forget to set the dictionary to Nothing when you have finished using it.

Set dict = Nothing
Mitch Wheat
This data structure type is provided by the scripting runtime, not by VBA. Basically, VBA can use practically any data structure type that is accessible to it via a COM interface.
David-W-Fenton
@David W. Fenton: ok I surrender!
Mitch Wheat
Just for the sake of completeness: you need to reference the "Microsoft Scripting Runtime" for this to work (go to Tools->References) and check its box.
regjo
+10  A: 

VBA does not have an internal implementation of a dictionary, but from VBA you can still use the dictionary object from MS Scripting Runtime Library.

Dim d
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "aaa"
d.Add "b", "bbb"
d.Add "c", "ccc"

If d.Exists("c") Then
    MsgBox d("c")
End If
Jarmo