对于range.formatconditions中的每个fc失败。为什么?

时间:2015-02-11 14:25:30

标签: excel vba excel-vba

以下代码适用于我的工作簿中的大多数工作表:

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

但它在For Each fc In .FormatConditions行上失败,错误消息类型不匹配的工作表上有rUsedRange.FormatConditions.Count = 34,其中许多都是图标集。

为什么代码在此表上失败?我该如何解决?

1 个答案:

答案 0 :(得分:6)

FormatConditions集合包括FormatConditionDataBarAboveAverageColorScaleUniqueValuesTop10和{{1对象,而不仅仅是IconSetCondition个对象,因此您需要将FormatCondition变量声明为fc