Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For each fc in range.formatconditions fails. Why?

Tags:

excel

vba

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?

like image 970
user1283776 Avatar asked Oct 27 '25 06:10

user1283776


1 Answers

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.

like image 91
Rory Avatar answered Oct 29 '25 21:10

Rory