Is there a way to call array member by key instead of index?

gifariz

Board Regular
Joined
May 2, 2021
Messages
112
Office Version
  1. 365
Platform
  1. Windows
I have been using an awkward method to store and call my UDT arrays by key.
That is by making an array and corresponding collection for storing the array index and calling the content by key, please see my code below.
Is there better practice for this in VBA? That maybe can call array member by key without making additional variable (collection) to store/call the array index by key.

VBA Code:
Type tSampleType
     Name As String
     nData As Long
     DataX() As Double
     DataY() As Boolean
End Type

Sub SampleSub()

     Dim ArrayA() As tSampleType
     Dim ArrayA_ID As New Collection
     Dim i As Long, Name As String, DataX As Double
     
     'Store array
     ReDim ArrayA(99)
     For i = 0 To 99
          With ArrayA(i)
               .Name = "abc" & i
               ArrayA_ID.Add i, .Name
               nData = 1000
               ReDim .DataX(nData - 1), .DataY(nData - 1)
               'and so on
          End With
     Next i

     'Calling array member by key
     Name = "abc0"
     i = ArrayA_ID(Name)
     DataX = ArrayA(i).DataX(123)

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I am not understand what UDT arrays doing, but using 2D array may helps

VBA Code:
Dim ArrayA(), i&, j&, Name as String
Redim ArrayA( 1 to 100, 1 to 4) ' array with 100 members down and 4 items accross (1 member & 3 items)
For i = 1 to 100
    For j = 1 to 4 
          ArrayA(i,1) = "abc" & i
          ArrayA(i,2) = ...
          ArrayA(i,3) = ...
          ArrayA(i,4) = ...
   Next
Next

'Get member2, items 3
Debug.print ArrayA(2,3)
 
Upvote 0
I am not understand what UDT arrays doing, but using 2D array may helps

VBA Code:
Dim ArrayA(), i&, j&, Name as String
Redim ArrayA( 1 to 100, 1 to 4) ' array with 100 members down and 4 items accross (1 member & 3 items)
For i = 1 to 100
    For j = 1 to 4
          ArrayA(i,1) = "abc" & i
          ArrayA(i,2) = ...
          ArrayA(i,3) = ...
          ArrayA(i,4) = ...
   Next
Next

'Get member2, items 3
Debug.print ArrayA(2,3)
UDT is for storing complex structured data, like array inside array, with clear naming/key of each sub variable.
In your code, the array is called by index (e.g. 2, 3) instead of key/name like what I need.
 
Upvote 0
UDT is for storing complex structured data, like array inside array, with clear naming/key of each sub variable.
In your code, the array is called by index (e.g. 2, 3) instead of key/name like what I need.
Another solution is using dictionary (for unique members) or collection, with members stored as key, and an array (or another dictionary or collection) stored as items.
Like this (collection sample)
VBA Code:
Dim col as new collection
Dim arrayA 
For i = 1 to 100
    arrayA = array("a","b","c","d")
   col.add i, arrayA

To get the final working code, try to upload an actual sample sheet.
 
Upvote 0
Solution
Another solution is using dictionary (for unique members) or collection, with members stored as key, and an array (or another dictionary or collection) stored as items.
Like this (collection sample)
VBA Code:
Dim col as new collection
Dim arrayA
For i = 1 to 100
    arrayA = array("a","b","c","d")
   col.add i, arrayA

To get the final working code, try to upload an actual sample sheet.
Thanks for your answer. Sorry for late reply.

So I now understand that collection and dictionary can have array or another dictionary/collection as items.

One more question:
Is it true that collection or dictionary can't have UDT as item?
Because I really need UDT for storing my data, it is very convenient and clear for complex structured data.

I got this error when trying it:

VBA Code:
Type tSubData
    SubDataA As Double
    SubDataB As Double
End Type

Type tMainData
    Name As String
    nData As Long
    DataX() As tSubData
End Type

Sub xx()

Dim VarA As tMainData

Dim myDict As Scripting.Dictionary
Set myDict = New Scripting.Dictionary

Dim myColl As New Collection

VarA.Name = "Name1"

myDict.Add "Name1", VarA
myColl.Add VarA, "Name1"

End Sub

Sorry I cant upload sample sheet because the actual code is too long, it is actually an API to process data from other software so other people can't run it anyway.
 
Upvote 0

Forum statistics

Threads
1,215,139
Messages
6,123,264
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top