搜索不存在的数据

时间:2015-07-14 16:54:55

标签: excel

我有两组数据范围。一个范围是Sheet 1(8000行,6列),它是主范围,另一个范围是Sheet 2(5000行,6列)。如何在新工作表(工作表3)上显示不在工作表2中但在工作表1中的其他3000行?

1 个答案:

答案 0 :(得分:0)

我可以把它变成一个宏,但首先,让我知道这是否会做你正在寻找的东西。

以下是我使用的样本数据,分为三页。

表1称为" Master"如下所示,从A1开始:

Name            Birthday Month     Age
Batman          January            35
Superman        March              32
Catwoman        April              37
Joker           June               28
Harley Quinn    September          33
Rorschach       July               35
Dr. Manhattan   August             41
Aquaman         October            22
Ms. Jupiter     October            23
Penguin         July               39

在Sheet2中:

Name            Birthday Month     Age
Batman          January            35
Superman        March              32
Catwoman        April              37
Joker           June               28
Harley Quinn    September          33
Rorschach       July               35

因此,正如您所看到的,我们需要将曼哈顿博士添加到(目前为空白)Sheet3中。

检查工作表2中的NOT是什么的快速方法是在主工作表中添加辅助列,并简单计算每个记录在Sheet2中显示的次数。在主表上,将此公式输入D2并复制=Countifs(Sheet2!$A:$A,A2,Sheet2!$B:$B,B2,Sheet2!$C:$C,C2)

这将为您留下记录在工作表2中显示的次数。如果您看到" 0",则表示您需要将这些记录添加到Sheet3中:

enter image description here

你去吧。

注意:由于你有大量的行,你可能会得到交替的1和0。要快速确定Sheet2中包含哪个避风港,只需按D列排序所有数据("带标题"):

enter image description here (注意:我最初选择范围A1:D11,但由于我选择"有标题",Excel会将明显选择的范围放一行。点击"确定"以及所有" ; 0"报告位于顶部,您可以将这些报告添加到工作表3中。

我可以把它变成一个宏,但是如果你愿意先告诉我它是如何起作用的。谢谢!

编辑:包含宏,如果您需要任何调整,请告诉我们!

  Sub Find_Reports()
    Dim lastRow As Long, lastCol As Integer, missingLastRow As Integer
    ' Create variables for the master worksheet, the list wsht (which has SOME of the Master records), and missing WS which we will put the missing records
    Dim masterWS As Worksheet, listWS As Worksheet, missingWS As Worksheet
    Dim listWSName As String
Dim ws as Worksheet

Application.DisplayAlerts= false
For each ws in Sheets
     If ws.name = "Missing" then ws.delete
Next ws
Application.DisplayAlerts= True

    Set masterWS = Sheets("Master List") ' Change this to whatever your Master List sheet is called
    Set listWS = Sheets("Sheet2") ' same with Master, change this to whatever the second sheet (that has SOME of the data) is called
    listWSName = listWS.Name

    Sheets.Add.Name = "Missing"
    Set missingWS = Sheets("Missing")
    missingWS.Rows(1).Value = masterWS.Rows(1).Value
    missingLastRow = 2 'Start on row 2

    lastRow = masterWS.Cells(1, 1).End(xlDown).Row 'I am assuming you have NO gaps in your column A, if you do, uncomment and use the next line instead
    'lastRow = masterWS.UsedRange.Rows.Count

    lastCol = masterWS.Cells(1, 1).End(xlToRight).Column

    Dim countCol As Integer
    countCol = lastCol + 1 'We will add the COUNT formula to this column
    masterWS.Cells(1, countCol).Value = "Times included in " & listWSName
    With masterWS
        .Range(.Cells(2, countCol), .Cells(lastRow, countCol)).FormulaR1C1 = "=COUNTIFS(" & listWSName & "!C[-3],RC[-3]," & listWSName & "!C[-2],RC[-2]," & listWSName & "!C[-1],RC[-1])"
    End With

    'Now, we would add the rows with a "0" next to it in Master WS to the new "Missing" WS
    Dim i As Integer
    With masterWS
        For i = 2 To lastRow
            If .Cells(i, countCol).Value = 0 Then 'If there is no record, add to the missing WS
                missingWS.Range(missingWS.Cells(missingLastRow, 1), missingWS.Cells(missingLastRow, lastCol)).Value = _
                    .Range(.Cells(i, 1), .Cells(i, lastCol)).Value
                missingLastRow = missingLastRow + 1
            End If
        Next i
    End With


    End Sub