Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 更新存储在VBA字典中的数组_Arrays_Excel_Vba_Data Structures_Dictionary - Fatal编程技术网

Arrays 更新存储在VBA字典中的数组

Arrays 更新存储在VBA字典中的数组,arrays,excel,vba,data-structures,dictionary,Arrays,Excel,Vba,Data Structures,Dictionary,我正在创建一个数据结构,它使用嵌套字典和最低级别的列表。以下是我的数据示例: Country, Customer, Purchased US, Alan, Lawnmower US, Alan, Hammer US, Karen, Donkey US, Simon, Mustang MX, Carl, Lawnmower MX, Alan, Donkey ... 我心目中的数据结构看起来像字典-->字典-->数组——也就是说,国家-->客户-->购买的。计划是为每个字典-->字典组合提供一个新

我正在创建一个数据结构,它使用嵌套字典和最低级别的列表。以下是我的数据示例:

Country, Customer, Purchased
US, Alan, Lawnmower
US, Alan, Hammer
US, Karen, Donkey
US, Simon, Mustang
MX, Carl, Lawnmower
MX, Alan, Donkey
...
我心目中的数据结构看起来像
字典-->字典-->数组
——也就是说,
国家-->客户-->购买的
。计划是为每个
字典-->字典
组合提供一个新数组

但是,当我尝试更新数组时,它似乎链接到
字典-->字典
结构的所有较低级别。也就是说,在处理第三行之后,出现以下情况:

US --> Alan --> [Lawnmower, Hammer, Donkey]
US --> Karen --> [Lawnmower, Hammer, Donkey]
。。。而我希望看到的是:

US --> Alan --> [Lawnmower, Hammer]
US --> Karen --> [Donkey]
以下是我尝试使用的代码:

i_p = UBound(purchased_array)

Redim Preserve purchased_array(i_p + 1)

purchased_array(i+p + 1) = item ' new item to add to the array

dataset(country)(customer) = purchased_array
但是,这会导致
字典-->字典
结构的每个最低级别引用基本相同的数组


有没有关于我做错了什么的想法?

字典里有几对。此外,字典中的键列表必须是唯一的。在包含国家/地区和客户关系的顶级词典中,根据该标准,国家/地区和客户都不是唯一的。所以,我认为字典不合适。也许你可以试试
Dictionary->Array->Array

如果字典中有数组,则必须先将其从字典中取出,然后才能对其进行修改。然后把它放回去

Sub Tester()

Dim x As Long, y As Long
Dim dict As New Scripting.Dictionary
Dim d As Scripting.Dictionary
Dim arr

    For x = 1 To 3
        Set d = New Scripting.Dictionary
        For y = 1 To 3
            d.Add "nextkey" & y, Array("A_" & x & "_" & y, _
                                       "B_" & x & "_" & y)
        Next y
        dict.Add "key" & x, d
    Next x

    Debug.Print Join(dict("key1")("nextkey1"), ", ") '>> A_1_1, B_1_1

    'try to modify array while stored in dictionary...
    dict("key1")("nextkey1")(1) = "newValue1" '<<< doesn't work!

    Debug.Print Join(dict("key1")("nextkey1"), ", ") '>> A_1_1, B_1_1

    'have to pull it out of the dictionary if you want to change it...
    arr = dict("key1")("nextkey1")
    arr(1) = "newValue2"
    dict("key1")("nextkey1") = arr

    Debug.Print Join(dict("key1")("nextkey1"), ", ") '>> A_1_1, newValue2

End Sub
子测试仪()
尺寸x和长度一样,y和长度一样
Dim dict作为新的脚本编写字典
Dim d作为脚本。字典
暗淡的边缘
对于x=1到3
Set d=New Scripting.Dictionary
对于y=1到3
d、 添加“nextkey”和“y”,数组(“A_x&”和“y”_
“B_ux&”x&y)
下一个y
添加“键”和x,d
下一个x
Debug.Print Join(dict(“key1”)(“nextkey1”),“,”)>>A_1_1,B_1_1
'存储在字典中时尝试修改数组。。。
dict(“key1”)(“nextkey1”)(1)=“newValue1”'A_1_1,B_1_1
“如果你想改变它,就必须把它从字典里取出来。。。
arr=dict(“key1”)(“nextkey1”)
arr(1)=“新值2”
dict(“key1”)(“nextkey1”)=arr
Debug.Print Join(dict(“key1”)(“nextkey1”),“,”>>A_1_1,newValue2
端接头

为什么
字典-->字典-->数组
,为什么不
字典-->集合
?我相信集合是一个键控数据结构?我可以用它代替数组吗?当然,集合对象支持
.Add(Item,[Key])
方法,但Key是可选的。所以,你可以省略它。在您的情况下,使用collection而不是array会简单得多。如果字典中有数组,则必须先将其从字典中取出,然后才能修改它。然后把它放回去。@TimWilliams当你说“拉出”的时候re:a array,你在说什么方法?我试着将数组复制到临时文件。数组,然后插入临时值。数组,但具有相同的最终结果。是否有一种方法可以将数组设置为NULL或类似的值?好的,我想我会按照您所做的来做。但是如果要扩展
arr
数组,会发生什么?也就是说,它应该是一个动态数组,根据购买列中的条目数增长/收缩。一旦它超出字典范围,您可以像使用常规数组一样使用它-如果您想扩展它,那么这应该不会有问题。