比较VBA中两个词典之间的数据

时间:2018-05-22 20:46:27

标签: vba excel-vba dictionary excel

我正在尝试比较两个词典中的数据,以便识别具有不同项目的键。我在编写循环和if语句时遇到困难,以使其发生。

在下面的示例中,它包含两个词典 - BeginningData EndingData 。 Key" Jane Jones"每个字典中都有一个不同的项目。

我在这种情况下寻找的输出是:" Jane Jones在第一本字典中是222222222,在第二本字典中是444444444"

关于如何完成此代码的任何想法?我试图避免在这个特定项目中的数组。谢谢!

Sub MainProcess()

Dim BeginningData As Dictionary, EndingData As Dictionary

Dim StartItem As Variant, EndItem As Variant

Set BeginningData = New Dictionary
Set EndingData = New Dictionary

With BeginningData
    .Add "John Smith", 111111111
    .Add "Jane Jones", 222222222
    .Add "Jim Brown", 333333333
End With

With EndingData
    .Add "John Smith", 111111111
    .Add "Jane Jones", 444444444
    .Add "Jim Brown", 333333333
End With

For Each StartItem In BeginningData.Keys
    For Each EndItem In EndingData.Keys

        'Some sort of if statement here
            Debug.Print _
            GetKey(BeginningData, BeginningData.Item(EndItem)) & _
            " is " & _
            BeginningData.Item(StartItem) & _
            " in the first dictionary, but is " & _
            EndingData.Item(StartItem) & _
            " in the second dictionary."
        End If

    Next
Next

End Sub

Function GetKey(Dic As Dictionary, strItem As String) As String
    Dim key As Variant
    For Each key In Dic.Keys
        If Dic.Item(key) = strItem Then
            GetKey = CStr(key)
            Exit Function
        End If
    Next
End Function

2 个答案:

答案 0 :(得分:1)

您既不需要嵌套循环也不需要GetKey()函数,只需使用Exists()对象的Dictionary方法来检查包含给定键的字典:

Option Explicit

Sub WorkingProcess()
    Dim BeginningData As Dictionary, EndingData As Dictionary

    Dim BeginningItem As Variant

    Set BeginningData = New Dictionary
    Set EndingData = New Dictionary

    With BeginningData
        .Add "John Smith", 111111111
        .Add "Jane Jones", 222222222
        .Add "Jim Brown", 333333333
    End With

    With EndingData
        .Add "John Smith", 111111111
        .Add "Jane Jones", 444444444
        .Add "Jim Brown", 333333333
    End With

    For Each BeginningItem In BeginningData.Keys
        If EndingData.Exists(BeginningItem) Then ' if current "beginning" dict key is in "ending" dict
            If BeginningData.Item(BeginningItem) <> EndingData.Item(BeginningItem) Then ' if items don't match
                Debug.Print _
                BeginningItem & _
                " is " & _
                BeginningData.Item(BeginningItem) & _
                " in the first dictionary, but is " & _
                EndingData.Item(BeginningItem) & _
                " in the second dictionary."
            End If
        End If
    Next
End Sub

答案 1 :(得分:0)

比较两个词典并生成我需要的输出的完整工作代码是:

Sub WorkingProcess()

Dim BeginningData As Dictionary, EndingData As Dictionary

Dim StartItem As Variant, EndItem As Variant

Set BeginningData = New Dictionary
Set EndingData = New Dictionary

With BeginningData
    .Add "John Smith", 111111111
    .Add "Jane Jones", 222222222
    .Add "Jim Brown", 333333333
End With

With EndingData
    .Add "John Smith", 111111111
    .Add "Jane Jones", 444444444
    .Add "Jim Brown", 333333333
End With

For Each StartItem In BeginningData.Keys
    For Each EndItem In EndingData.Keys

        If GetKey(BeginningData, BeginningData.Item(StartItem)) = _
        GetKey(BeginningData, BeginningData.Item(EndItem)) And _
        BeginningData.Item(StartItem) <> EndingData.Item(StartItem) Then
            Debug.Print _
            GetKey(BeginningData, BeginningData.Item(EndItem)) & _
            " is " & _
            BeginningData.Item(StartItem) & _
            " in the first dictionary, but is " & _
            EndingData.Item(StartItem) & _
            " in the second dictionary."
        End If

    Next
Next

End Sub

Function GetKey(Dic As Dictionary, strItem As String) As String
    Dim key As Variant
    For Each key In Dic.Keys
        If Dic.Item(key) = strItem Then
            GetKey = CStr(key)
            Exit Function
        End If
    Next
End Function