[VBA] Add array to the Dictionary and read from it

skorpionkz

Well-known Member
Joined
Oct 1, 2013
Messages
1,171
Office Version
  1. 2016
Hi guys,

Could some one explain me how can I correctly add array to the dictionary and then read from it? I dont want to add every element of array, I want to save whole array. I have this code to create dictionary of the arrays:

Rich (BB code):
Public dicRefData As New Scripting.Dictionary

Public Sub MyReference()

Dim arrMetric As Variant, arrData As Variant, vKey As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim lRow As Long, lCol As Long, l As Long

Set wb = SetWorkbook("ref_Data.csv")
Set ws = wb.ActiveSheet

With ws
    lCol = .Rows(1).Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
    lRow = .Cells.Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    
    arrData = .Range(.Cells(1, 1), .Cells(lRow, lCol)).Value2
End With
wb.Close SaveChanges:=False

For l = LBound(arrData, 1) + 1 To UBound(arrData, 1)
    If Not arrData(l, 1) = Empty Then dicRefData(arrData(l, 1)) = Empty
Next l

Debug.Print dicRefData.Count
lRow = 0
For Each vKey In dicRefData.Keys
Erase arrMetric
    For l = LBound(arrData, 1) + 1 To UBound(arrData, 1)
        lRow = lRow + 1
        ReDim Preserve arrMetric(1 To 1, 1 To lRow)
               
        If arrData(l, 1) = vKey Then
            arrMetric(1, lRow) = arrData(l, 5) & " " & arrData(l, 6)
        End If
    Next l
Set dicRefData (vKey) = arrMetric
Next vKey

End Sub

I am not sure if this is correct and if it is how to read an array from dictionary?
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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