【问题标题】:VBA how to access dictionary Keys elements directlyVBA如何直接访问字典键元素
【发布时间】:2019-08-15 10:11:57
【问题描述】:

上下文

在 VBA 中,您可以创建和填充字典,例如:

    Dim oDict As Object
    Set oDict = CreateObject("Scripting.Dictionary")
    oDict("key 1") = "value 1"
    oDict("key 2") = "value 2"
    oDict("key 3") = "value 3"

使用Keys method,您可以将键作为数组获取:

返回一个包含 Dictionary 对象中所有现有键的数组。

    Dim aKeys() As Variant
    aKeys = oDict.Keys
    Debug.Print VarType(aKeys)       ' prints "8204"
    Debug.Print VarType(oDict.Keys)  ' prints "8204"

问题

但是当我直接访问其中一个密钥时,它会给出这个神秘的错误消息:

    Debug.Print aKeys(2)       ' prints "key 3"
    Debug.Print oDict.Keys(2)  ' Run-time error '451':
                               ' Property let procedure not defined 
                               ' and property get procedure did not 
                               ' return an object

尝试解决失败

虽然以上是我不理解的主要行为,但在尝试将oDict.Keys 作为数组处理的完整列表下方:

Option Explicit

Public Function ArrayGet(ByRef aArray() As Variant, i As Integer) As Variant
    ArrayGet = aArray(i)
End Function

Public Function ArrayWrap(ByRef aArray() As Variant) As Variant()
    ArrayWrap = aArray
End Function

Public Sub TEST()

    Dim oDict As Object
    Set oDict = CreateObject("Scripting.Dictionary")
    oDict("key 1") = "value 1"
    oDict("key 2") = "value 2"
    oDict("key 3") = "value 3"

    Dim aKeys() As Variant
    aKeys = oDict.Keys

    Debug.Print VarType(aKeys)           ' prints "8204"
    Debug.Print VarType(oDict.Keys)      ' prints "8204"

    Debug.Print aKeys(2)                 ' prints "key 3"
    'Debug.Print oDict.Keys(2)            ' Run-time error '451': Property let procedure not defined and property get procedure did not return an object
    'Debug.Print oDict.Keys.Get(2)        ' Run-time error '424': Object required
    'Debug.Print oDict.Keys.Item(2)       ' Run-time error '424': Object required

    Debug.Print ArrayGet(aKeys, 2)       ' prints "key 3"
    'Debug.Print ArrayGet(oDict.Keys, 2)  ' Compile error : Type mismatch: array or user-defined type expected

    'Debug.Print Array(aKeys)(2)          ' Run-time error '9'  : Subscript out of range
    'Debug.Print Array(oDict.Keys)(2)     ' Run-time error '9'  : Subscript out of range

    Debug.Print ArrayWrap(aKeys)(2)      ' prints "key 3"
    'Debug.Print ArrayWrap(oDict.Keys)(2) ' Compile error : Type mismatch: array or user-defined type expected

    Dim key As Variant

    For Each key In aKeys
        Debug.Print key                  ' prints "key 1", "key 2" and "key 3"
    Next key

    For Each key In oDict.Keys
        Debug.Print key                  ' prints "key 1", "key 2" and "key 3"
    Next key

End Sub

【问题讨论】:

  • 来自 OP 的注释:编写问题的过程实际上让我自己找到了答案。希望这个问题中包含的关键字可以让其他人更快地找到解决方案。
  • 来自 OP 的注意事项:既然知道原因,很容易看出它与 covered here 是同一个根本问题

标签: arrays excel vba dictionary


【解决方案1】:

出了什么问题

    Debug.Print oDict.Keys(2)  ' Run-time error '451':
                               ' Property let procedure not defined 
                               ' and property get procedure did not 
                               ' return an object

Keys 这个词是一种方法。虽然 VBA 允许您在不提供参数时删除括号,但它仍然是一种方法。如果您在其后面指定括号,则内容将传递给该方法。 Keys 方法不接受整数参数。

如何解决

通过显式提供 Keys 方法的括号(如Keys()),我们可以直接应用/跟随括号来访问数组元素。

与问题中的示例一致:以下两种替代方案是等效的:

    Debug.Print aKeys(2)         ' prints "key 3"
    Debug.Print oDict.Keys()(2)  ' prints "key 3"

【讨论】:

  • 感谢您分享您的发现。这已经困扰了我很多年了...我通常在即时窗口中查看我的字典和字典,这是今天发生在我身上的最好的事情...天哪...谢谢!!!
【解决方案2】:

如果不添加对 Microsoft Scripting Runtime 的引用,则需要使用后期绑定才能使用字典对象。使用后期绑定,您会失去一些功能。

如果您尝试按索引访问字典的键,则会抛出运行时错误 451。

运行时错误“451”:
属性让过程未定义,属性获取过程未返回对象。

如果您通过 VBE 的工具、引用将 Microsoft Scripting Runtime 库引用添加到项目中,您现在可以通过其序号索引号直接访问字典的键。

Sub test3()

    Dim i As Long, dict As New Scripting.dictionary

    dict.Add Key:="key1", Item:="item1"
    dict.Add Key:="key2", Item:="item2"
    dict.Add Key:="key3", Item:="item3"

    For i = LBound(dict.Keys) To UBound(dict.Keys)
        Debug.Print dict.Keys(i)
    Next i

End Sub

'results
key1
key2
key3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2013-03-24
    • 2016-03-16
    相关资源
    最近更新 更多