VBA判断某列数据重复存在

来源:互联网 发布:单片机定时器中断程序 编辑:程序博客网 时间:2024/05/22 02:30

如果数据重复存在,则将重复数据标红色并提示        

①数据行数确定时:

For i = 2 To 10    ' 数据检查数据从第2行到第10行的数据

        If Application.CountIf(Range("A1:A10" ), Cells(i, 1)) > 1 Then

                Cells(i, 1).Font.Color = 255  
                Cells(i, 5) = Sheet1.Cells(i, 1) + "重复存在"

        End If

 Next i


②数据行数不确定时:

For i = 2 To totalRow     ' totalRow   数据的总行数,可利用查询函数动态获取。

        If Application.CountIf(Range("A1:A" & totalRow), Cells(i, 1)) > 1 Then

                Cells(i, 1).Font.Color = 255
                Cells(i, 5) = Sheet1.Cells(i, 1) + "重复存在"

        End If

 Next i


演示结果如下图: