|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
对当前工作表按第一列重复值标识,运行前备份源数据是良好习惯。
本帖最后由 书法爱好者 于 2025-4-2 21:31 编辑
Sub 颜色标识重复值() Dim ws As Worksheet Dim rng As Range Dim lastRow As Long, lastCol As Long Dim dict As Object Dim cell As Range Dim key As String Dim i As Long, j As Long Set ws = ActiveSheet lastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, 1)) rng.FormatConditions.Delete Set dict = CreateObject("Scripting.Dictionary") Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Application.EnableEvents = False dict.RemoveAll If Application.CountA(ws.Columns(1)) > 0 Then For i = 1 To lastRow If Not IsEmpty(ws.Cells(i, 1)) Then key = CStr(ws.Cells(i, 1).Value) If dict.Exists(key) Then dict(key) = dict(key) + 1 Else dict.Add key, 1 End If End If Next i For i = 1 To lastRow If Not IsEmpty(ws.Cells(i, 1)) Then key = CStr(ws.Cells(i, 1).Value) If dict(key) > 1 Then ws.Cells(i, 1).Interior.Color = RGB(255, 199, 206) ws.Cells(i, 1).Font.Color = RGB(156, 0, 6) End If End If Next i End If Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic Application.EnableEvents = True MsgBox "处理完成!共处理 " & lastRow & " 行数据。" End Sub |
|