Create and Extract The Data from Dictionary
Dictionary
If Dict.Exists("Banana") Then
MsgBox Dict("Banana")
End If
Dim KeyValue As Variant
For Each KeyValue In Dict.Keys
MsgBox Dict(KeyValue)
Next
Dim i As Long
For i = 0 To Dict.Count - 1
msgbox Dict.Items(i)
Next
Sub RetrieveValues_From_Dictionary()
On Error Resume Next
'======Define Worksheet=========
Dim SH As Worksheet
Set SH = ThisWorkbook.Sheets("Dictionary Structure")
'======Define Dictionary=========
Dim SalesDict As Scripting.Dictionary
Set SalesDict = New Scripting.Dictionary
'=========Add Values to Dictionary===
LastRow = SH.Range("A" & Rows.Count).End(xlUp).Row
For r = 2 To LastRow
K = SH.Range("A" & r).Value
Jan = SH.Range("B" & r).Value
Feb = SH.Range("C" & r).Value
Mar = SH.Range("D" & r).Value
April = SH.Range("E" & r).Value
May = SH.Range("F" & r).Value
SalesDict.Add Key:=K, Item:=Array(Jan, Feb, Mar, April, May)
Next
'============Extract the Data From Dictionary=====
KeyValue = SH.Range("J4").Value
'MsgBox SalesDict.Item(KeyValue)(1)
For i = 0 To UBound(SalesDict.Item(KeyValue))
SH.Cells(4, i + 11).Value = SalesDict.Item(KeyValue)(i)
Next
EraseDictionary
End Sub
Function EraseDictionary()
Set SalesDict = Nothing
End Function