VBA重复/空单元格-突出显示

时间:2019-02-07 22:29:41

标签: excel vba colors duplicates highlight

我需要为重复项创建类似的宏(请参见下文)。用户将能够选择列符号,然后所选列中的所有重复项将以颜色突出显示。我不怎么做。

以下是相同的想法,但单元格为空。

您能帮忙吗? THX!

Sub EmptyCells()

    Dim kol As String
    Dim ost As Long

    ost = Cells(Rows.Count, "A").End(xlUp).Row
    kol = InputBox("Enter column symbol: B, C...etc.", "Column symbol", "B")

    If kol = vbNullString Then Exit Sub
    If IsNumeric(kol) Then
        MsgBox "You entered number, please enter column symbol", _
                vbInformation, "ERROR"
        Exit Sub
    End If
    If ost < 5 Then Exit Sub

    Range("A5:E" & ost).Interior.Color = xlNone

    Range(Cells(5, kol), Cells(ost, kol)).SpecialCells(xlCellTypeBlanks).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

End Sub

1 个答案:

答案 0 :(得分:0)

请根据程序中的注释进行以下更改。更改后对我有用。假设我已经清除了工作表上的现有格式,那么这将是第一种格式条件。

Sub DuplicateCells() ' changed sub name

    Dim kol As String
    Dim ost As Long

    ost = Cells(Rows.Count, "A").End(xlUp).Row
    kol = InputBox("Enter column symbol: B, C...etc.", "Column symbol", "B")

    If kol = vbNullString Then Exit Sub
    If IsNumeric(kol) Then
        MsgBox "You entered number, please enter column symbol", _
                vbInformation, "ERROR"
        Exit Sub
    End If
    If ost < 5 Then Exit Sub

    Range("A5:E" & ost).Interior.Color = xlNone

    Range(Cells(5, kol), Cells(ost, kol)).Select  ' Remove SpecialCells(xlCellTypeBlanks)
    Selection.FormatConditions.AddUniqueValues   'Add this line
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority      'Add this line
    Selection.FormatConditions(1).DupeUnique = xlDuplicate   'Add this line

    With Selection.FormatConditions(1).Interior   '    add FormatConditions(1)
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

duplicate values highlighted显示了我的样本数据的结果。