存储可由Key VBA访问的多维数据

时间:2014-08-26 16:17:11

标签: python vba excel-vba data-structures excel

我需要一个VBA中的数据结构,这将允许我使用for循环迭代一个投资列表,并通过"投资名称"然后存储"市场价值"和"百分比回报"与那个"投资名称"配对。我在想象一个数组字典,但是读到VBA字典不允许包含数组。我希望能够调用investmentDictionary.item("投资名称")(0或1)= 0将是市场价值,1将是百分比回报。

我可以通过字典键访问非常重要,因为我将更新电子表格中不断变化的数据,因此我不能依赖索引号。我对编码一般不太熟悉,所以我要感谢所涉及的特定语法的一个例子。我也知道我可以用一个多维数组来完成这个任务,我在第一个维度上迭代以找到名称,但我想知道它与字典方法相比有多高效。我很感激任何见解。

- 德雷克

1 个答案:

答案 0 :(得分:1)

如果我理解你,那就是你想要的

  Dim dict As Object


  Set dict = CreateObject("Scripting.Dictionary")

  'create an array with the number of values you want to add to a specific key. You can do it in a loop as well'
  a = Array("Market_Value", "Percent_Return", "Whatever")

  'add the values to the key'
  If Not dict.Exists(Key) Then
      dict.Add "investment name", a
  End If

 For Each Key In dict
     'returns investment name'
     MsgBox Key

     'returns Market Value, Percent Return, Whatever'
     MsgBox Join(dict.Item(Key))

     'if you want to access the inner elements of the value list'
      b = Split(Join(dict.Item(Key)), " ")

      'returns Market_Value'
      MsgBox b(0)


      'returns Percent_Return'
       MsgBox b(1)
 Next Key

如果您正在寻找的是什么,请不要忘记接受答案。