Excel vba:将vlookup结果添加到字典不';行不通

Excel vba:将vlookup结果添加到字典不';行不通,vba,excel,Vba,Excel,我有一个Scripting.Dictionary对象,它在for循环中填充值对。问题是,VLookup查找值并在调试ok中打印出来 Dim indexes As Object Set indexes = CreateObject("Scripting.Dictionary") Dim tempYear As Integer For tempYear = 2009 To year indexes.Add Applica

我有一个
Scripting.Dictionary
对象,它在for循环中填充值对。问题是,
VLookup
查找值并在调试ok中打印出来

Dim indexes As Object
            Set indexes = CreateObject("Scripting.Dictionary")
            Dim tempYear As Integer
            For tempYear = 2009 To year
    indexes.Add Application.WorksheetFunction.VLookup("TEXT", _
statsWorkbook.Worksheets("economic").Range(rangeString2), tempYear - 1999, 0), tempYear  'here's the adding


            Debug.Print "inside tempYear cycle"
            Debug.Print "vlookup: " & Application.WorksheetFunction.VLookup("TEXT", _
statsWorkbook.Worksheets("economic").Range(rangeString2), tempYear - 1999, 0)   'prints out the right value

            Debug.Print indexes(tempYear)  'doesn't print anything
            Next
编辑:现在,我试图简化示例:

Sub testDICT()
    Dim indexes As Object
    Set indexes = CreateObject("Scripting.Dictionary")

    indexes.Add "Lazy boy", "cat"
    indexes.Add "Good boy", "dog"
    indexes.Add "Evil boy", "bear"

    MsgBox (indexes("cat"))
End Sub

而且我也无法获取值。

在向词典添加项目时,您已交换了值和键。看见在示例代码中,它应该是:

Sub testDICT()
    Dim indexes As Object
    Set indexes = CreateObject("Scripting.Dictionary")

    indexes.Add "cat", "Lazy boy"
    indexes.Add "dog", "Good boy"
    indexes.Add "bear", "Evil boy"

    MsgBox (indexes("cat"))
End Sub
或者在您的真实代码中:

Dim indexes As Object
            Set indexes = CreateObject("Scripting.Dictionary")
            Dim tempYear As Integer
            For tempYear = 2009 To Year
    indexes.Add tempYear, Application.WorksheetFunction.VLookup("TEXT", _
statsWorkbook.Worksheets("economic").Range(rangeString2), tempYear - 1999, 0)  'here's the adding


            Debug.Print "inside tempYear cycle"
            Debug.Print "vlookup: " & Application.WorksheetFunction.VLookup("TEXT", _
statsWorkbook.Worksheets("economic").Range(rangeString2), tempYear - 1999, 0)   'prints out the right value

            Debug.Print indexes(tempYear)  'doesn't print anything
            Next

让我知道这是否解决了您的问题。

在向词典添加项目时,您已交换了值和键。看见在示例代码中,它应该是:

Sub testDICT()
    Dim indexes As Object
    Set indexes = CreateObject("Scripting.Dictionary")

    indexes.Add "cat", "Lazy boy"
    indexes.Add "dog", "Good boy"
    indexes.Add "bear", "Evil boy"

    MsgBox (indexes("cat"))
End Sub
或者在您的真实代码中:

Dim indexes As Object
            Set indexes = CreateObject("Scripting.Dictionary")
            Dim tempYear As Integer
            For tempYear = 2009 To Year
    indexes.Add tempYear, Application.WorksheetFunction.VLookup("TEXT", _
statsWorkbook.Worksheets("economic").Range(rangeString2), tempYear - 1999, 0)  'here's the adding


            Debug.Print "inside tempYear cycle"
            Debug.Print "vlookup: " & Application.WorksheetFunction.VLookup("TEXT", _
statsWorkbook.Worksheets("economic").Range(rangeString2), tempYear - 1999, 0)   'prints out the right value

            Debug.Print indexes(tempYear)  'doesn't print anything
            Next

让我知道这是否解决了您的问题。

Debug.Print index.Item(tempYear)
-如果这样做不起作用,请尝试将查找分配给字符串变量,然后将字符串变量添加到字典中。@braX它没有改变任何内容。而且,据我所知,不应该这样做,因为
字典(key)
是一种完全有效的语法。什么样的结果不起作用?MsgBox(index(“Lazy boy”))Cat是值,而不是键。您只能使用键检索值,如上所示。
Debug.Print index.Item(tempYear)
-如果这样做不起作用,请尝试将查找分配给字符串变量,然后将字符串变量添加到字典中。@braX它没有更改任何内容。而且,据我所知,不应该这样做,因为
字典(key)
是一种完全有效的语法。什么样的结果不起作用?MsgBox(index(“Lazy boy”))Cat是值,而不是键。只能使用键检索值,如上所示。