从工作簿中的多个工作表中查找重复项

时间:2018-08-12 16:09:00

标签: excel excel-vba excel-formula

我一张纸上有60万条数据。在“列I”中,我在Sheet1,sheet2,sheet3和sheet4中有电话号码。我想比较工作表中的重复项并突出显示其中的重复值。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

这将突出显示工作表中的重复项。您可以使用简单的Conditional Formatting在同一张纸上标注重复项。


更新:如果每个工作表具有10,000个相同的行,则在关闭ScreenUpdating的情况下,宏需要2分钟(准确的说是156.4063秒)来运行。这意味着在此时序测试中突出显示了30,000个单元格。


Option Explicit

Sub Duplicate_Digits()

Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")
Dim ws3 As Worksheet: Set ws3 = ThisWorkbook.Sheets("Sheet3")
Dim Numbers1, Numbers2, Numbers3, i
Dim Found As Range

Numbers1 = ws1.Range("I2:I" & ws1.Range("I" & ws1.Rows.Count).End(xlUp).Row).Value
Numbers2 = ws2.Range("I2:I" & ws2.Range("I" & ws2.Rows.Count).End(xlUp).Row).Value
Numbers3 = ws3.Range("I2:I" & ws3.Range("I" & ws3.Rows.Count).End(xlUp).Row).Value

For i = LBound(Numbers2, 1) To UBound(Numbers2, 1)
    Set Found = ws1.Range("I:I").Find(Numbers2(i, 1))
        If Not Found Is Nothing Then
            Found.Interior.Color = vbYellow
        End If
    Set Found = Nothing
Next i

For i = LBound(Numbers3, 1) To UBound(Numbers3, 1)
    Set Found = ws1.Range("I:I").Find(Numbers3(i, 1))
        If Not Found Is Nothing Then
            Found.Interior.Color = vbYellow
        End If
    Set Found = Nothing
Next i

For i = LBound(Numbers1, 1) To UBound(Numbers1, 1)
    Set Found = ws2.Range("I:I").Find(Numbers1(i, 1))
        If Not Found Is Nothing Then
            Found.Interior.Color = vbYellow
        End If
    Set Found = Nothing
Next i

For i = LBound(Numbers3, 1) To UBound(Numbers3, 1)
    Set Found = ws2.Range("I:I").Find(Numbers3(i, 1))
        If Not Found Is Nothing Then
            Found.Interior.Color = vbYellow
        End If
    Set Found = Nothing
Next i

For i = LBound(Numbers1, 1) To UBound(Numbers1, 1)
    Set Found = ws3.Range("I:I").Find(Numbers1(i, 1))
        If Not Found Is Nothing Then
            Found.Interior.Color = vbYellow
        End If
    Set Found = Nothing
Next i

For i = LBound(Numbers2, 1) To UBound(Numbers2, 1)
    Set Found = ws3.Range("I:I").Find(Numbers2(i, 1))
        If Not Found Is Nothing Then
            Found.Interior.Color = vbYellow
        End If
    Set Found = Nothing
Next i

End Sub

答案 1 :(得分:0)

我不确定这会占用多大的资源,如果其他人认为它不能在OP的60万行上使用,请发出提示。


您可以在帮助器列中使用=COUNTIFS()来标记重复的值。

将此公式放在K1中并自动填充到底部:

=COUNTIFS(A:A,A1)

如果公式显示1不是重复项,如果显示任何值>1则表明重复项。