在单个列中突出显示重复的值

时间:2019-03-18 07:28:41

标签: excel vba

我正在尝试检查G列中的重复项,如果有重复项,请以红色突出显示。我写了下面的查询,但它不起作用,并以

的形式显示错误消息
  

无法获取WorksheetFunction类的CountIf属性

Dim Rng1 As Range
Dim g As Range

Set Rng1 = Range(Range("G11"), Range("G" & Rows.Count).End(xlUp))

For Each g In Rng1
    If WorksheetFunction.CountIf(Rng1, g.Value) > 1 Then
         g.Interior.ColorIndex = 3
    End If
Next g

1 个答案:

答案 0 :(得分:0)

尝试:

Option Explicit

Sub test()

    Dim rng As Range, cell As Range

    With ThisWorkbook.Worksheets("Sheet1")

        Set rng = .Range("G11:G" & .cells(.Rows.Count, "G").End(xlUp).Row)

        For Each cell In rng
            If WorksheetFunction.CountIf(rng, cell) > 1 Then
                cell.Interior.Color = vbRed
            End If
        Next cell

    End With

End Sub