获取条件格式的单元格(DupeUnique = xlDuplicate)

时间:2019-01-21 13:08:26

标签: excel vba

我将使用以下内容突出显示所选内容中所有重复的单元格:

Selection.FormatConditions.AddUniqueValues
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
With Selection.FormatConditions(1).Font
    .Color = -16383844
    .TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 13551615
    .TintAndShade = 0
End With

现在,我不仅要突出显示那些单元格,还想获得一个包含所有受影响单元格的数组。我试图遍历我的选择并检查Interior属性,但这需要花一些时间。我正在寻找更快的方式。

1 个答案:

答案 0 :(得分:0)

一个函数,返回Excel Selection中所有值的列表(集合),对应于以下条件:

If myCell.DisplayFormat.Interior.Color = myColor Then

将非常有用:

Sub TestMe()

    '...OP Code
        .TintAndShade = 0
    End With

    Dim unique As Collection
    Set unique = ResultList(Selection)
    If unique.Count > 1 Then Debug.Print unique.Item(2)

End Sub

Public Function ResultList(selectedRange As Range, _
            Optional myColor As Long = 13551615) As Collection

    Dim myCell As Range
    Dim myResultList As New Collection

    For Each myCell In selectedRange
        If myCell.DisplayFormat.Interior.Color = myColor Then
            myResultList.Add myCell.Value2
        End If
    Next myCell
    Set ResultList = myResultList

End Function

因此,避免使用Selection,并且可以进一步使用它。