回答
把答案在這裏以作記錄,從reddit的用戶MRMCMLXXXV
源https://www.reddit.com/r/excel/comments/6u4swi/how_do_you_create_a_dictionary_in_excel_vba_and/
值Public Sub DictionaryExamples()
Dim exampleValues As Variant
Dim i As Long
Dim aKey As String
Dim aValue As Integer
Dim exampleDict As Object
'Load values into a variant array
exampleValues = Range("A1:B10").Value
'Instantiate a dictionary
Set exampleDict = CreateObject("scripting.dictionary")
'Read all keys and values, and add them to the dictionary
For i = 1 To UBound(exampleValues)
aKey = CStr(exampleValues(i, 1))
aValue = CInt(exampleValues(i, 2))
exampleDict.Add aKey, aValue
Next i
'Output the value associated with key A
MsgBox exampleDict.Item("A")
End Sub
結果看起來像在Excel
在Excel:
=VLOOKUP("D", A:B, 2,FALSE)
返回20
。 在VBA:
MsgBox WorksheetFunction.VLookup("D", Sheet1.Range("A:B"), 2, False)
彈出20
。
難道只是更容易使用vlookups的一切,而不是字典?或者我錯過了什麼? (例如,它不是計算效率高嗎?) – Kagerjay
真的取決於您的應用程序。有時我會使用VLookups,有時候會使用字典,有時候還會使用別的。 –
你也可以使用散列表匹配...(只是在開玩笑!) –
- 1. Excel VBA - 詞典 - 存儲和檢索值
- 2. VBA-excel詞典
- 3. excel vba詞典vlookup
- 4. 在Excel中檢查日期VBA詞典
- 5. Excel VBA +查找與詞典
- 6. Excel VBA HTML檢索
- 7. Excel VBA詞典刪除項目
- 8. 在Excel VBA詞典中更新值
- 9. 檢索詞典在C#
- 10. 檢索字典從孤立的存儲
- 11. 使用VBA和Excel檢索epay.info餘額
- 12. VBA:詞典 - 只能檢索最後一個條目
- 13. 更新存儲在VBA詞典中的數組
- 14. 存儲和檢索樹
- 15. 存儲和檢索數據
- 16. 存儲和檢索高分
- 17. 存儲和檢索NSAttributedString
- 18. 存儲和檢索QFlags
- 19. 存儲和檢索cookie
- 20. 密碼存儲和檢索
- 21. Excel 2010 vs Excel 2013 VBA存儲過程
- 22. 如何在Swift中存儲和檢索字典值類型
- 23. 創建一個哈希來存儲和檢索英語動詞
- 24. 檢索存儲
- 25. Excel VBA保存/恢復上一個搜索詞
- 26. 字典詞典 - vba Office for Mac
- 27. Python的 - 使用詞典來檢查查詢和檢索答案
- 28. NSUserDefaults.StandardUserDefaults - 保存和檢索字典
- 29. 存儲和檢索搜索結果
- 30. 如何存儲和檢索neo4j索引
是的,循環,除非你更喜歡使用'WorksheetFunction.VLookup'並忘記Dictionary。 –
你可以發佈一個示例代碼模板,做同樣的事情:D(與查找) – Kagerjay