create dyanamic array with string references vba

Hi,  I would like to create an array and reference each object within the array as a string:

for example arr("a") = 1, arr("b") = 2

where "a" and "b" are not constants.

Does anyone know how to do this?

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
This is commonly known as a HashTable where you access items by "key".

In VB you can use the Collection or Dictionary (with a reference to the "Microsoft Scripting Runtime") objects.

Here is a simple example...

    Dim values As New Collection
    Dim strVal As String
    Dim intVal As Integer
    strVal = "a"
    intVal = 1
    values.Add intVal, strVal
    strVal = "b"
    intVal = 2
    values.Add intVal, strVal
    MsgBox values("a")
    strVal = "b"
    Dim val As Variant
    val = values(strVal)
    MsgBox val

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.