Excel双向列表比较摘要

时间:2017-12-15 15:09:39

标签: excel vlookup

我正在对一组Excel列表进行双向比较,以检查列表2中是否存在列表1中的ID(反之亦然)。列表2中缺少的任何ID都是Dropout。列表2中添加的任何新ID都是New。两个列表中的任何ID均为Returning

我尝试使用IF和VLOOKUP的组合。

=IF(ISERROR(VLOOKUP(sheet1!A2,sheet2!A:A,1,FALSE)),"Dropout","Returning")

单向比较(检查丢失)是有效的。我需要一种检查新学生的方法(相反方向的比较)。如何在同一个摘要栏中执行此操作?

更新#1:

摘要表的B列:C中的值是从sheet1中提取的。那些需要一个公式来从正确的表格中智能地选择正确的数据。

更新#2:

似乎Visual Basic脚本可能是解决此问题的更顺畅的方法。有关如何处理此问题的任何指示?

1级出勤:

list 1

2级出勤:

list 2

要点:

summary

3 个答案:

答案 0 :(得分:3)

仅仅因为:

Sub outpt()
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim arr1 As Variant, arr2 As Variant, outarr As Variant
Dim dict As Object
Dim i As Long, j As Long
Dim st As Boolean, nd As Boolean
Dim item As Variant

Set dict = CreateObject("scripting.Dictionary")

Set ws1 = Worksheets("Sheet1") 'Class 1
Set ws2 = Worksheets("Sheet2") 'Class 2
Set ws3 = Worksheets("Sheet3") 'Summary

With ws1
    arr1 = .Range(.Cells(2, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value
End With
With ws2
    arr2 = .Range(.Cells(2, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value
End With

For i = LBound(arr1, 1) To UBound(arr1, 1)
    On Error Resume Next
        dict.Add arr1(i, 1), arr1(i, 2)
    On Error GoTo 0
Next i

For i = LBound(arr2, 1) To UBound(arr2, 1)
    On Error Resume Next
        dict.Add arr2(i, 1), arr2(i, 2)
    On Error GoTo 0
Next i

ReDim outarr(1 To dict.Count, 1 To 3) As Variant
j = 1
For Each item In dict.keys
    st = False
    nd = False
    For i = LBound(arr1, 1) To UBound(arr1, 1)
        If arr1(i, 1) = item Then
            st = True
            Exit For
        End If
    Next i
    For i = LBound(arr2, 1) To UBound(arr2, 1)
        If arr2(i, 1) = item Then
            nd = True
            Exit For
        End If
    Next i
    outarr(j, 2) = item
    outarr(j, 3) = dict(item)
    If st And nd Then
        outarr(j, 1) = "Returning"
    ElseIf st Then
        outarr(j, 1) = "Dropout"
    Else
        outarr(j, 1) = "New"
    End If
    j = j + 1
Next item
'Assumes titles already in row 1
ws3.Range("A2").Resize(dict.Count, 3).Value = outarr


End Sub

创建:

enter image description here

答案 1 :(得分:1)

一个简单的技巧可能是以这种方式复制数据:

1级2级

2级1级

并从第一列到第二列执行VLOOKUP将得到结果。

答案 2 :(得分:1)

您可以简单地合并两个公式:

=IF(ISERROR(VLOOKUP(Sheet3!B2,Sheet2!A:A,1,FALSE)),"Dropout",IF(ISERROR(VLOOKUP(Sheet3!B2,Sheet1!A:A,1,FALSE)),"New","Returning"))

(假设1级出勤在Sheet1上,2级出勤在Sheet2上,摘要在表3上)

Result

但我个人更喜欢更具可读性的东西(比如NIVeR的回答)。

当然,斯科特克拉纳的建议更好。