Dictionary with array as the value

Shamusvw

New Member
Joined
Feb 1, 2017
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi, I have been battling to understand why an array is not being updated where the array is in place of a key/value pair.
I have the following in VBA:
VBA Code:
Dim testDict As Object
Dim testKey As String
Dim arr(1) As Double
Set testDict = CreateObject("Scripting.Dictionary")
testKey = "TestKey"
arr(0) = 64
arr(1) = 0
testDict.Add testKey, arr
Debug.Print "Before: "; testDict(testKey)(0)
testDict(testKey)(0) = testDict(testKey)(0) + 126.5
Debug.Print "After: "; testDict(testKey)(0)

And in my Immediate Window I get the following:
Before: 64
After: 64

However, my expectation is that I would get:
Before: 64
After: 190.5
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You cannot change the value in the array directly like that, try it like
VBA Code:
   Dim testDict As Object
   Dim testKey As String
   Dim arr(1) As Double
   Dim Tmp As Variant
   
   Set testDict = CreateObject("Scripting.Dictionary")
   testKey = "TestKey"
   arr(0) = 64
   arr(1) = 0
   testDict.Add testKey, arr
   Debug.Print "Before: "; testDict(testKey)(0)
   Tmp = testDict(testKey)
   Tmp(0) = Tmp(0) + 126.5
   
   testDict(testKey) = Tmp
   Debug.Print "After: "; testDict(testKey)(0)
 
Upvote 0
Solution
Can't tell you exactly why it doesn't work, but you could workaround it like this:

Editing the original array:
VBA Code:
    Dim testDict As Object
    Dim testKey As String
    Dim arr(1) As Double
    
    Set testDict = CreateObject("Scripting.Dictionary")
    testKey = "TestKey"
    arr(0) = 64
    arr(1) = 0
    testDict.Add testKey, arr
    
    Debug.Print "Before: "; testDict(testKey)(0)
    arr(0) = testDict(testKey)(0) + 126.5
    testDict(testKey) = arr
    Debug.Print "After: "; testDict(testKey)(0)

Storing the array in a variable:
VBA Code:
    Dim testDict As Object
    Dim testKey As String
    Dim arr(1) As Double
    Dim tempArr As Variant
    
    Set testDict = CreateObject("Scripting.Dictionary")
    testKey = "TestKey"
    arr(0) = 64
    arr(1) = 0
    testDict.Add testKey, arr
    
    Debug.Print "Before: "; testDict(testKey)(0)
    tempArr = testDict(testKey)
    tempArr(0) = tempArr(0) + 126.5
    testDict(testKey) = tempArr
    Debug.Print "After: "; testDict(testKey)(0)
 
Upvote 0
You cannot change the value in the array directly like that, try it like
VBA Code:
   Dim testDict As Object
   Dim testKey As String
   Dim arr(1) As Double
   Dim Tmp As Variant
  
   Set testDict = CreateObject("Scripting.Dictionary")
   testKey = "TestKey"
   arr(0) = 64
   arr(1) = 0
   testDict.Add testKey, arr
   Debug.Print "Before: "; testDict(testKey)(0)
   Tmp = testDict(testKey)
   Tmp(0) = Tmp(0) + 126.5
  
   testDict(testKey) = Tmp
   Debug.Print "After: "; testDict(testKey)(0)
Apologies, Fluff. Hadn't seen you had already responded to this when I posted.
 
Upvote 0
Thank you, I appreciate the feedback. I've implemented the solution by Fluff
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,974
Members
449,095
Latest member
Mr Hughes

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