将两列与另外两列进行比较,找到匹配项和不匹配项

时间:2017-07-24 16:31:28

标签: excel excel-vba vba

我正在试图弄清楚如何遍历两个数组,将第二个数组中的所有值与第一个数组中的所有值进行比较,并且对于每个匹配,测试D列中同一行中的值和H列相同。

在我的第一个数组中,我有这样的数据。 enter image description here

在我的第二个数组中,我有这样的数据。 enter image description here

因此,对于OW细胞,我想比较

OW&报告实体,并确保两者都是VARCHAR。这将是这些字段名称的相同数据类型的匹配。另外,对于OW&货币,都是VARCHAR,所以再次匹配。对于OW& CID_CounterpartyID,我看到Integer和VARCHAR,所以这不是匹配,我想知道这一点。

我试图把一些东西放在一起做这个,我在下面显示我的代码,但这肯定不起作用。

Sub CompareFinal()

    Dim r1 As Range
    Dim r2 As Range
    Dim cell As Range
    Dim lastrow As Long

    ' All Rows in First Array
    With ThisWorkbook.Worksheets("Data Validation")
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set r1 = .Range("A2:A" & lastrow)
    End With

    ' All rows in Second Array
    With ThisWorkbook.Worksheets("Data Validation")
        lastrow = .Cells(.Rows.Count, "E").End(xlUp).Row
        Set r2 = .Range("E2:E" & lastrow)
    End With

    ' Loop through Second Array, which will bequual to or longer than First Array
    For Each cell In r2
        If IsError(Application.Match(cell, r1, 0)) Then
            cell.Offset(, 5) = "Match"
        Else
            cell.Offset(, 5) = "NoMatch"
        End If
    Next cell

End Sub

我确信我过度简化了代码中的内容,这给了我不正确的结果。有人可以帮助我理顺吗?我认为,非代码功能或VBA解决方案都可以完成这项工作。

感谢。

1 个答案:

答案 0 :(得分:0)

我刚刚意识到我实际上需要匹配另外两列。下面的功能似乎做了我想要的。

=ISNUMBER(MATCH(B2 & "|" & C2 & "|" &D2,$E$2:$E$800 & "|" & $H$2:$H$800 & "|" & $I$2:$I$800,0))

我发现此链接非常有用。

Compare two columns if another two columns are matching in excel macro

希望这有助于其他人。

相关问题