VBA遍历字典键和项

时间:2019-09-19 09:12:52

标签: excel vba dictionary

菜鸟问题。我知道以前可能已经问过这个问题,但是我仍然需要一些帮助,因为我从未在VBA中使用过字典。

我找到了一段不错的代码,它将列变成字典,而这正是我所需要的(见下文),但是一旦将范围转换成字典,我就不知道该怎么做。甚至简单的Debug.Print或循环遍历键值。我浏览了无数的文章和youtube视频(解释了词典,但没有“高级”),我缺少了一些东西。

Sub Test()
    RangeToDict2 Range("H2:I36")

    Debug.Print RangeToDict2.Item("a") 'FAILS HERE
End Sub
Function RangeToDict2(ByVal R As Range) As Dictionary
    Set RangeToDict2 = New Dictionary
    i = 1
    Do Until i >= (R.Rows.Count * R.Columns.Count)
        RangeToDict2.Add R(i), R(i + 1)
        Debug.Print R(i) & ", " & R(i + 1)
        i = i + 2
    Loop
End Function

1 个答案:

答案 0 :(得分:1)

您应该阅读braX建议的有关脚本字典的一两个教程。

您的示例中的一个特殊问题是080491c2 <win>: 80491c2: 55 push %ebp 80491c3: 89 e5 mov %esp,%ebp 80491c5: 81 ec 28 01 00 00 sub $0x128,%esp 80491cb: 8b 4d 08 mov 0x8(%ebp),%ecx 80491ce: 89 8d e0 fe ff ff mov %ecx,-0x120(%ebp) 80491d4: 8b 4d 0c mov 0xc(%ebp),%ecx 80491d7: 89 8d e4 fe ff ff mov %ecx,-0x11c(%ebp) 80491dd: 8b 8d e0 fe ff ff mov -0x120(%ebp),%ecx 80491e3: 81 f1 55 da b4 14 xor $0x14b4da55,%ecx 80491e9: 89 c8 mov %ecx,%eax 80491eb: 8b 8d e4 fe ff ff mov -0x11c(%ebp),%ecx 80491f1: 80 f5 00 xor $0x0,%ch 80491f4: 89 ca mov %ecx,%edx 80491f6: 09 d0 or %edx,%eax 80491f8: 85 c0 test %eax,%eax 80491fa: 75 09 jne 8049205 <win+0x43> 80491fc: 81 7d 10 be b4 0d f0 cmpl $0xf00db4be,0x10(%ebp) 8049203: 74 1a je 804921f <win+0x5d> 8049205: 83 ec 0c sub $0xc,%esp 8049208: 68 08 a0 04 08 push $0x804a008 804920d: e8 4e fe ff ff call 8049060 <puts@plt> 8049212: 83 c4 10 add $0x10,%esp 8049215: 83 ec 0c sub $0xc,%esp 8049218: 6a 01 push $0x1 804921a: e8 51 fe ff ff call 8049070 <exit@plt> 804921f: 83 ec 0c sub $0xc,%esp 8049222: 68 1e a0 04 08 push $0x804a01e 8049227: e8 34 fe ff ff call 8049060 <puts@plt> 804922c: 83 c4 10 add $0x10,%esp 804922f: 83 ec 08 sub $0x8,%esp 8049232: 68 27 a0 04 08 push $0x804a027 8049237: 68 29 a0 04 08 push $0x804a029 804923c: e8 5f fe ff ff call 80490a0 <fopen@plt> 8049241: 83 c4 10 add $0x10,%esp 8049244: 89 45 f4 mov %eax,-0xc(%ebp) 8049247: 83 7d f4 00 cmpl $0x0,-0xc(%ebp) 804924b: 75 12 jne 804925f <win+0x9d> 804924d: 83 ec 0c sub $0xc,%esp 8049250: 68 34 a0 04 08 push $0x804a034 8049255: e8 06 fe ff ff call 8049060 <puts@plt> 804925a: 83 c4 10 add $0x10,%esp 804925d: eb 31 jmp 8049290 <win+0xce> 804925f: 83 ec 04 sub $0x4,%esp 8049262: ff 75 f4 pushl -0xc(%ebp) 8049265: 68 00 01 00 00 push $0x100 804926a: 8d 85 f4 fe ff ff lea -0x10c(%ebp),%eax 8049270: 50 push %eax 8049271: e8 da fd ff ff call 8049050 <fgets@plt> 8049276: 83 c4 10 add $0x10,%esp 8049279: 83 ec 08 sub $0x8,%esp 804927c: 8d 85 f4 fe ff ff lea -0x10c(%ebp),%eax 8049282: 50 push %eax 8049283: 68 86 a0 04 08 push $0x804a086 8049288: e8 a3 fd ff ff call 8049030 <printf@plt> 804928d: 83 c4 10 add $0x10,%esp 8049290: 90 nop 8049291: c9 leave 8049292: c3 ret 08049293 <vuln>: 8049293: 55 push %ebp 8049294: 89 e5 mov %esp,%ebp 8049296: 83 ec 18 sub $0x18,%esp 8049299: 83 ec 0c sub $0xc,%esp 804929c: 68 90 a0 04 08 push $0x804a090 80492a1: e8 8a fd ff ff call 8049030 <printf@plt> 80492a6: 83 c4 10 add $0x10,%esp 80492a9: 83 ec 0c sub $0xc,%esp 80492ac: 8d 45 e8 lea -0x18(%ebp),%eax 80492af: 50 push %eax 80492b0: e8 8b fd ff ff call 8049040 <gets@plt> 80492b5: 83 c4 10 add $0x10,%esp 80492b8: 83 ec 08 sub $0x8,%esp 80492bb: 8d 45 e8 lea -0x18(%ebp),%eax 80492be: 50 push %eax 80492bf: 68 a0 a0 04 08 push $0x804a0a0 80492c4: e8 67 fd ff ff call 8049030 <printf@plt> 80492c9: 83 c4 10 add $0x10,%esp 80492cc: 90 nop 80492cd: c9 leave 80492ce: c3 ret 080492cf <main>: 80492cf: 8d 4c 24 04 lea 0x4(%esp),%ecx 80492d3: 83 e4 f0 and $0xfffffff0,%esp 80492d6: ff 71 fc pushl -0x4(%ecx) 80492d9: 55 push %ebp 80492da: 89 e5 mov %esp,%ebp 80492dc: 51 push %ecx 80492dd: 83 ec 04 sub $0x4,%esp 80492e0: a1 34 c0 04 08 mov 0x804c034,%eax 80492e5: 6a 00 push $0x0 80492e7: 6a 02 push $0x2 80492e9: 6a 00 push $0x0 80492eb: 50 push %eax 80492ec: e8 9f fd ff ff call 8049090 <setvbuf@plt> 80492f1: 83 c4 10 add $0x10,%esp 80492f4: e8 9a ff ff ff call 8049293 <vuln> 80492f9: b8 00 00 00 00 mov $0x0,%eax 80492fe: 8b 4d fc mov -0x4(%ebp),%ecx 8049301: c9 leave 8049302: 8d 61 fc lea -0x4(%ecx),%esp 8049305: c3 ret 是一个带有RangeToDict2参数的函数,该函数返回一个字典,因此Range并不像Debug.Print RangeToDict2.Item("a")那样有意义不是范围。您可能需要更多类似这样的东西:

Item("a")

然后您可以遍历Sub Test() Dim d As Object Set d = RangeToDict2(Range("A1:B3")) Debug.Print d.Item("a") 'FAILS HERE End Sub 的键或项目。