[Original] VBA study notes (313) VBA dictionary Related: traverse the dictionary, with key check item, check the item with key approach

 

1 dictionary traversal methods

  • Even late binding, you can also use 
  • for each i in dict1.keys () 'dict1.keys used herein (), but not dict1.keys (i) is a
Sub test_dict11()

Dim dict1 As Object
Set dict1 = CreateObject("scripting.dictionary")


dict1.Add 1, "h"
dict1.Add 2, "e"
dict1.Add 3, "l"
dict1.Add 4, "l"
dict1.Add 5, "o"
dict1.Add 6, "world"

For Each I In dict1.keys()
   Debug.Print I & "," & dict1(I)
Next
Debug.Print
'知道key 去查item

Debug.Print dict1(1)
Debug.Print dict1.Item(2)
Debug.Print



End Sub

 

2 with the search key item, normal operation

  • dict1(key)
  • dict1.items(key)

 

3 with the key item search operations unconventional

3.1 My own thoughts: dict1.keys () and dict1.items () is a pair of corresponding

  • Using the key, item design is paired dictionary
  • If the item is not repeated, then the index is the key item of the index, the index is synchronized
  • Even repeated item, the screen may be a plurality of loop index
  • Note, however, this method must be early-bound to take effect. Because only early binding is supported dict1.keys (i) of this cycle

 

  • 1 unabated on the error, because the problem of match function is a worksheet function
  • Finding such a match, or the need to support early binding because dict1.keys (index) must be directly binding preliminary
Sub test_dict1()

Dim dict1 As New Dictionary

dict1.Add 1, "h"
dict1.Add 2, "e"
dict1.Add 3, "l"
dict1.Add 4, "l"
dict1.Add 5, "o"
dict1.Add 6, "world"

'方法1
'下面的反查方法要生效,必须是前期绑定!!!
'知道item去查key
'利用key,item 应该是成对的,也就是分别在 keys() items()的index是一样的。
For J = LBound(dict1.Items()) To UBound(dict1.Items())
    If dict1.Items(J) = "world" Then
'     If dict1(J) = "h" Then
       Debug.Print J             'keys(),items()数组没定义,index是从0开始的
       Debug.Print dict1.Keys(J)
    End If

Next
Debug.Print

'方法2,也只有前期绑定支持
'不减1就出错,因为match函数的问题,是工作表函数
'这样用match查找,还是需要前期绑定的支持,因为 dict1.keys(index) 直接用必须前期绑定
Debug.Print dict1.Keys(Application.Match("world", dict1.Items(), 0) - 1)


End Sub

 

 

4 Method 2, late binding is also active

  • Late binding can also check
  • If you are late binding
  • You can not directly use dict.keys () dict.items ()
  • But you can use an array to look around transit
arr1=dict.keys()
arr1(index)
arr2=dict.items()
arr2(index)

 

Sub test_dict22()
'后期绑定各种测试


Dim dict1 As Object
Set dict1 = CreateObject("scripting.dictionary")


dict1.Add 1, "h"
dict1.Add 2, "e"
dict1.Add 3, "l"
dict1.Add 4, "l"
dict1.Add 5, "o"
dict1.Add 6, "world"


'后期绑定,为什么又可以使用dict1.keys()? for each可以使用? 不能用index方式使用这2个数组?
For Each I In dict1.Keys()
    Debug.Print I & " ";
    Debug.Print dict1(I)
Next
Debug.Print



'数组中转
'dict1.keys()  dict1.items() 都可以使用,但不能做index for i 的这种遍历,中转的数组可以
'dict1里 key-item同步,只要item不重复,可以用相同的变量循环

arr1 = dict1.Keys()
arr2 = dict1.Items()
For I = LBound(arr2) To UBound(arr2)
    If arr2(I) = "world" Then
       Debug.Print arr1(I) & "," & arr2(I)
    End If
Next
Debug.Print
End Sub

 


 

Published 370 original articles · won praise 45 · views 90000 +

Guess you like

Origin blog.csdn.net/xuemanqianshan/article/details/103916083