Excel VBA:为什么只有在触发MsgBox后才能添加正确的字典值?

Excel VBA:为什么只有在触发MsgBox后才能添加正确的字典值?,excel,vba,dictionary,Excel,Vba,Dictionary,我在VBA中遇到了一个特殊的怪癖,我不确定是否有一个永久的解决方案。只有在触发MsgBox时,才会将值添加到我的字典中。我希望12345能被加到我的字典里。然而,我在下面的第一个例子中没有提到 For i = 2 To lastrow If (Not today.exists(.Cells(i, headers.Item("SL_ID")).Value) And _ .Cells(i, headers.Item("Assigned Date")).Value >= D

我在VBA中遇到了一个特殊的怪癖,我不确定是否有一个永久的解决方案。只有在触发MsgBox时,才会将值添加到我的字典中。我希望12345能被加到我的字典里。然而,我在下面的第一个例子中没有提到

For i = 2 To lastrow
    If (Not today.exists(.Cells(i, headers.Item("SL_ID")).Value) And _
       .Cells(i, headers.Item("Assigned Date")).Value >= Date - minusDays And _
       Len(.Cells(i, headers.Item("Assigned Status")).Value) > 3 And _
       InStr(LCase(.Cells(i, headers.Item("Assigned Status")).Value), "started") = 0) Then
           today.Add Key:=.Cells(i, headers.Item("SL_ID")).Value, _
            Item:=Array(.Cells(i, headers.Item("Assigned Status")).Value, _
                        .Cells(i, headers.Item("Completion Date")).Value, _
                        .Cells(i, headers.Item("On Hold Comments")).Value, _
                        "-", "-", "-", "-", "Rework", source)
    End If
Next i
但是下面使用MsgBox的代码确实如预期的那样工作,12345被添加到了我的字典中

For i = 2 To lastrow
    If (.Cells(i, headers.Item("SL_ID")).Value = "12345") Then
        MsgBox ("Wow, it's 12345")
    End If

    If (Not today.exists(.Cells(i, headers.Item("SL_ID")).Value) And _
       .Cells(i, headers.Item("Assigned Date")).Value >= Date - minusDays And _
        Len(.Cells(i, headers.Item("Assigned Status")).Value) > 3 And _
        InStr(LCase(.Cells(i, headers.Item("Assigned Status")).Value), "started") = 0) Then
                today.Add Key:=.Cells(i, headers.Item("SL_ID")).Value, Item:=Array(.Cells(i, headers.Item("Assigned Status")).Value, .Cells(i, headers.Item("Completion Date")).Value, .Cells(i, headers.Item("On Hold Comments")).Value, "-", "-", "-", "-", "Rework", source)
    End If
Next i
有人知道为什么会这样吗?暂停打开MsgBox是否允许Excel“赶上”并正确添加?这是一个已知的问题吗


感谢您的帮助。显然,我的解决方案不是永久性的,我很想知道为什么会发生这种奇怪的事情。

字典的comparemode是什么?如果创建dict时未设置,则应为vbBinaryCompare。它已设置为vbBinaryCompare。仍然不走运。是否有多个12345值与其他条件匹配?一些12345的文本和其他文本是真实的数字吗?12345作为真数字和“12345”作为文本都可以添加到dict的keys.Nope中。未添加值12345。有趣的是,我只是做了一个分析,在I=3651之后,任何具有匹配条件的行都没有被添加。如果我添加一个If语句,如:If(I=3651),那么MsgBox(I)End If将按照预期继续通过循环。你以前见过这样的事吗?还有件事你没有告诉我们。