2

I am using a scripting dictionary in VBA to hold 1,840,000 key and value pairs in Excel 2010. I want to declare and populate them once, then use them in my modules and functions.

For starters, I declared the dictioanry as public, using Public dict As Scripting.Dictionary, then I populated it on worksheet_activate(). How do I save it and use it in other mods?

Thank you

7
  • either public in a module as ash said, either pass it as argument to the Subs/functions. The good stuff would be to not lose the dictionaries on break/error... Commented Jun 20, 2017 at 23:36
  • @A.S.H I named a public sub "workbook_activate()" and it didn't run when the workbook opened. however, when I used "worksheet_activate()" it was great as long as my mods were still in that sheet/mod file. Once I left, it appears the values within the dictionary get wiped. Commented Jun 20, 2017 at 23:39
  • @PatrickLepelletier are you implying that normally the dictionary would wipe on an error? Commented Jun 20, 2017 at 23:40
  • @A.S.H workbook.open* Commented Jun 20, 2017 at 23:46
  • Anything which resets your VBA project (eg an unhandled error, using End) will result in any global/static variables being lost. Commented Jun 21, 2017 at 0:05

1 Answer 1

8

As a good programming practice, wrap such a variable in a getter function, and hide the global variable as a static local inside it. Populate it on first use.

To do that, write this function inside a standard module:

Public Function getMyDictionary() as Scripting.Dictionary
    Static dict as Dictionary ' static: will keep state across different calls

    If dict Is Nothing Then
        Set dict = new Scripting.Dictionary
        ''''''''''''''''''''
        dict.Add "foo", "bar"
        ' etc...
        ' Code to populate dictionary
        '
        ''''''''''''''''''''
    End If
    Set getMyDictionary = dict
End Function

Now whenever you need to reference the dictionary, simply type something like this:

If getMyDictionary.Exists("foo") Then doStuff

This idiom has many advantages:

  • the dictionary is populated on first use. If during an Excel session it is not needed, it will not be populated.

  • you no longer need to worry about "when do I populate my dictionary." It will be available and populated whenever and wherever it is needed

  • if the project unloads due to some runtime error, all global variables are reset. In particular, objects are reset to Nothing. The wrapper function handles correctly the situation and re-populates the dictionary in a transparent way.

  • TLDR... the global dictionary is hidden, access to it is (to some extent) controlled. "To some extent" because other code can still manipulate it (do insert, remove...). If one needs more control over it, for example allow other code to only read it, then make it a private member of some Class Module that exposes only the allowed functionality...

Sign up to request clarification or add additional context in comments.

10 Comments

How would it evaluate "foobar", what purpose does this add? Isn't/shouldn't a boolean getMyDictionary be enough?
@wizlog Great, but dont forget Set dict = new Scripting.Dictionary, I just added it :)
A.S.H., as soon as I run a sub located in another object (read: sheet) I loose access to the dictionary I just created and populated in the function you posted... despite it being static.
@wizlog what do you mean by you loose access? Make sure you put the function in a Standard Code Module, and always use it with the name getMyDictionary. Of course you can use another name for the function, but consider its name as the name of your dictionary in all other code.
Sorry for being unclear, and thank you for being so helpful. By "loose access" I mean "dict is nothing" would return true, and it would need to recreate itself. I simply assumed the space in memory was overridden.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.