2012-07-13 16 views
9

我正在使用VBA,並且需要將數據保存到key =>value以獲得最快速度;這種數據類型幫助我緩存來自http請求的響應文本,提高查詢速度。但我不知道最好的辦法是什麼?我需要一個與php數組類型相同的數據類型key=>value!感謝您的幫助!什麼是最好的VBA數據類型`key` =>`value`來保存與PHP數組相同的數據

+2

的可能重複的[是否有VBA字典結構?](http://stackoverflow.com/questions/915317/does-vba-have-dictionary-structure) – 2012-07-13 03:40:52

回答

14

你看過字典對象嗎?

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 

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

+0

謝謝!我明白了! – Davuz 2012-07-13 04:38:09

+2

請不要參考其他網站。這是您可以編寫解決方案的地方。 – 2015-10-13 21:14:45

+3

@PawelMiechowiecki:提供具有額外信息的URL有什麼問題?特別是因爲要使此代碼工作,您必須啓用對「Microsoft腳本運行時」的引用,該步驟在給定的URL中進行了描述... – 2017-04-05 16:30:14

相關問題