什么是最好的VBA数据类型`key` =>`value`来保存与PHP数组相同的数据

Dav*_*vuz 14 arrays vba list key-value

我正在使用VBA并且需要以type key=> 保存数据value以获得最快速度; 这种数据类型帮我从http请求缓存responese文本,提高查询速度.但我不知道最好的方法是什么?我需要一个与php数组相同的数据类型key=>value!谢谢你的帮助!

Irf*_*ino 20

你看过字典对象吗?

Sub DictExample1()

Dim dict As Dictionary
Dim v As Variant

    'Create the dictionary          
    Set dict = New Dictionary

   'Add some (key, value) pairs
    dict.Add "John", 34
    dict.Add "Jane", 42
    dict.Add "Ted", 402

    'How many items do we have?
    Debug.Print "Number of items stored: " & dict.Count

    'We can retrieve an item based on the key
    Debug.Print "Ted is " & dict.Item("Ted") & " years old"


   'We can test whether an item exists
    Debug.Print "We have Jane's age: " & dict.Exists("Jane")
    Debug.Print "We have Zak's age " & dict.Exists("Zak")

    'We can update a value by replacing it
   dict.Item("Ted") = dict.Item("Ted") / 10

    Debug.Print "Ted's real age is: " & dict.Item("Ted")

   'We can add more items
    dict.Add "Carla", 23

   'And we can iterate through the complete dictionary
    For Each v In dict.Keys
        Debug.Print "Name: " & v & "Age: "; dict.Item(v)
    Next

End Sub
Run Code Online (Sandbox Code Playgroud)

(来源:http://www.techbookreport.com/tutorials/vba_dictionary.html)

  • @PawelMiechowiecki:为URL提供额外信息有什么问题?特别是因为要使此代码工作,您必须启用对"Microsoft Scripting Runtime"的引用,该步骤在给定的URL中描述... (8认同)
  • 请不要参考其他网站.这是您可以编写解决方案的地方. (2认同)
  • 它告诉我对 Set dict = New Dictionary 无效使用 new Keyword (2认同)
  • 在参考中包含Microsoft脚本运行时将解决此问题 (2认同)