user1283776
user1283776

Reputation: 21764

For each fc in range.formatconditions fails. Why?

The following code works for most sheets in my workbook:

Function IsHighlighted() As Boolean

    Dim wks As Worksheet
    For Each wks In ThisWorkbook.Worksheets
        Dim rUsedRange As Range
        Set rUsedRange = wks.UsedRange

        With rUsedRange
            Dim bHighlighted As Boolean

            Dim fc As FormatCondition
            For Each fc In .FormatConditions
                If fc.Interior.Color = RGB(255, 0, 0) And fc.Font.Color = RGB(255, 255, 0) Then
                    bHighlighted = True
                    Exit For
                End If
            Next fc

            If bHighlighted = True Then
                Exit For
            End If
        End With
        Debug.Print (wks.Name & "," & rUsedRange.FormatConditions.count)
    Next wks

    IsHighlighted = bHighlighted
End Function

But it fails on the line For Each fc In .FormatConditions with the error message type mismatch on a worksheet that has rUsedRange.FormatConditions.Count = 34 out of which many are icon sets.

Why does the code fail on this sheet? How can I fix it?

Upvotes: 4

Views: 773

Answers (1)

Rory
Rory

Reputation: 34045

The FormatConditions collection includes FormatCondition, DataBar, AboveAverage, ColorScale, UniqueValues, Top10 and IconSetCondition objects, not just FormatCondition objects, so you need to declare your fc variable as Object.

Upvotes: 7

Related Questions