V查找同一个人多个日期

VLookup the same person multiple dates

我有两个电子表格:

我想匹配两张表并确保每个人的日期都匹配。例如。第 1 个人有三个不同的日期,我想准确匹配它们 1:1。

第 1 个人,2015 年 3 月 2 日 (Table A) -> 第 1 个人,2015 年 3 月 2 日 (Table B)
第 1 个人,2015 年 3 月 5 日 (Table A) -> 第 1 个人,2015 年 3 月 5 日 (Table B)
第 1 个人,2015 年 3 月 6 日 (Table A) -> 第 1 个人,2015 年 3 月 6 日 (Table B)

目前我遍历 Table A 中的 "No." 列并在 Table B 上使用 Application.VLookup 但这仅在一个人只有一个日期时才有效。否则它与 Table B 中的第一个日期进行比较。参见代码:

For sheetArowCounter= 2 To sheetAlastRow
    Sheets("A").Select
    sheetAperson = Cells(sheetArowCounter, 1)
    sheetAdate = Cells(sheetArowCounter, 2)
    Sheets("B").Select
    sheetBdate = Application.VLookup(sheetAperson, _
         Sheets("B").Range(Cells(1, 1), Cells(sheetBLastRow, 2)), 2, False)
    If IsError(sheetBdate ) Then
        personFromTableAnotFound = personFromTableAnotFound + 1
    ElseIf sheetBdate <> sheetAdate Then
        sheetAdateNotMatched = sheetAdateNotMatched + 1
    End If
    sheetAdateCompared = sheetAdateCompared + 1
Next sheetArowCounter

知道我该怎么做吗?

我同意上面的吉普车。尝试使用 SUMIF。您甚至可以在 IF 语句中将 SUMIF 嵌套到 return 文本:即:"Match"、"No match"

我会选择 countifs 函数,它可以针对多列测试条件。以下是如何在代码中实现此功能的示例:

For sheetArowCounter = 2 To sheetAlastRow
    Sheets("A").Select
    sheetAperson = Cells(sheetArowCounter, 1)
    sheetAdate = Cells(sheetArowCounter, 2)
    Sheets("B").Select

    'using the countifs function eg. =COUNTIFS(B!A3:A11,A!A3,B!B3:B11,A!B3)
    PersonDateMatches = Application.WorksheetFunction.CountIfs(Sheets("B").Range(Cells(1, 1), Cells(sheetBLastRow, 1)), sheetAperson, Sheets("B").Range(Cells(1, 2), Cells(sheetBLastRow, 2)), sheetAdate)

    If PersonDateMatches = 0 Then
        personDateFromTableAnotFound = personDateFromTableAnotFound + 1
    End If
    sheetAdateCompared = sheetAdateCompared + 1
Next sheetArowCounter