【问题标题】:VBA Dictionary if exists?VBA字典是否存在?
【发布时间】:2015-11-17 20:31:24
【问题描述】:

我有一个名为 flArr 的数组,我将其传输到字典并测试另一个数组中的元素(如果存在),然后我将其添加到列表框中,但我不知道,不知何故它无法正常工作给我“需要对象”错误。有什么想法吗?。

Set dic_cc = CreateObject("scripting.dictionary")
dic_cc = flArr 'transferring array to dictionary 

    For Each f In heArr 'another array heArr        
        If dic_cc.Exists(f) Then Me.FilterList.AddItem f    
    Next

谢谢

【问题讨论】:

  • 您不能将数组分配给这样的字典 - 您需要遍历数组并逐个添加项目。
  • 是的,我可以。我可以遍历 dic_cc 并且我看到了该数组的所有元素。我在这一行只有问题 If dic_cc.Exists(f) Then Me.FilterList.AddItem f
  • 不,你不能。尝试声明 dic_cc As Object 看看会发生什么。您在当前代码中所做的是将变量 dic_cc 分配给数组 flArr - 此时它不再是 Dictionary 对象并变成数组。仅当您将 dic_cc 声明为 Variant(或至少不是 Object)时才有效。

标签: arrays vba dictionary


【解决方案1】:

我不认为 VBA 中的数组是可枚举的。您需要遍历数组中的每个元素并像这样检查字典:

dim i as long

For i= 0 to to ubound(myArray)
 If dic_cc.Exists(myArray[i]) then
  me.FilterList.AddItem myArray[i]
 endif

next i

【讨论】:

  • 声明Dim ar(1 To 3) As Integer,然后向其中添加一些项目。那么For Each it In ar有效,可以使用。
猜你喜欢
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 2017-09-28
相关资源
最近更新 更多