• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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?

1 Solution
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


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now