回答
把答案在这里以作记录,从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