Excel vba - Compare two ranges and find non matches

后端 未结 3 2048
广开言路
广开言路 2021-01-06 15:34

I\'ve got two Excel sheets where one sheets consists of a list of users. And the other list contains the same data, only the same user is listed several times. Now, I need s

3条回答
  •  情深已故
    2021-01-06 16:11

    You can use a simple MATCH formula to detect any non matches, then delete them with AutoFilter

    If your first list was in Sheet 1 Column A, your second in Sheet 2 Column A then in B1 of Sheet 2 put =ISNA(MATCH(A1,Sheet1!A:A,0)) and copy down

    this returns TRUE where the second list cant be matched against the first. You can then delete these TRUE rows with autofilter

    Note that you could also use =COUNTIF(Sheet1!A:A,A1)=0 for the same effect to identify not matches (as TRUE)

    xl2010 pic shown here

    enter image description here [VBA added]

    Sub QuickKill()
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim rng1 As Range
        Set ws1 = Sheets(1)
        Set ws2 = Sheets(2)
        ws2.Columns(2).Insert
        Set rng1 = ws2.Range(ws2.[a1], ws2.Cells(Rows.Count, "A").End(xlUp))
        Rows(1).Insert
        With rng1.Offset(0, 1)
            .FormulaR1C1 = "=COUNTIF('" & ws1.Name & "'!C1,RC[-1])=0"
            .AutoFilter Field:=1, Criteria1:="TRUE"
            .EntireRow.Delete
            .EntireColumn.Delete
        End With
    End Sub
    

提交回复
热议问题