【问题标题】:Storing multidimensional Data that can be accessed by Key VBA存储可通过 Key VBA 访问的多维数据
【发布时间】:2014-08-26 16:17:11
【问题描述】:

我需要一个 VBA 中的数据结构,它允许我使用 for 循环遍历投资列表并按“投资名称”存储它们,然后存储“市场价值”和“百分比回报”配对那个“投资名”。我在想象一个数组字典,但读到 VBA 字典不允许包含数组。我希望能够调用 InvestmentDictionary.item("investment name")(0 or 1) = 0 将是市场价值,1 将是百分比回报。

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

--德雷克

【问题讨论】:

    标签: python vba excel data-structures


    【解决方案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
    

    如果这就是您要寻找的,请不要忘记接受答案。

    【讨论】:

    • 感谢您的快速回复。不过,我可能对这个问题的措辞很差。我看到您的示例将允许我以“市场价值”为键访问“回报”。如果“市值”未知,我需要使用投资名称同时访问“市值”和“回报”怎么办?问题源于将两个项目与一个键配对。
    • 我更改了我的代码。请看看,让我知道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2020-06-14
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2011-05-24
    • 2013-04-06
    相关资源
    最近更新 更多