Excel VBA模块不会忽略空白单元格

时间:2015-09-29 18:44:26

标签: excel-vba vba excel

我有一个VBA模块,我上网计算带有条件格式的单元格。此模块的问题在于,如果在计数范围内,单元格为空白或没有条件格式规则,则会返回错误。该模块是:

Function CountCFCells(rng As Range, C As Range)
Dim i As Single, j As Long, k As Long
Dim chk As Boolean, Str1 As String, CFCELL As Range
chk = False
For i = 1 To rng.FormatConditions.Count
    If rng.FormatConditions(i).Interior.ColorIndex = C.Interior.ColorIndex Then
        chk = True
        Exit For
    End If
Next i
j = 0
k = 0
If chk = True Then
    For Each CFCELL In rng
        Str1 = CFCELL.FormatConditions(i).Formula1
        Str1 = Application.ConvertFormula(Str1, xlA1, xlR1C1)
        Str1 = Application.ConvertFormula(Str1, xlR1C1, xlA1, , ActiveCell.Resize(rng.Rows.Count, rng.Columns.Count).Cells(k + 1))
        If Evaluate(Str1) = True Then j = j + 1
        k = k + 1
    Next CFCELL
Else
CountCFCells = "Color not found"
Exit Function
End If
CountCFCells = j
End Function

当我使用=CountCFCells(A1:A30, B1)调用此函数时,我希望它忽略任何单元格没有任何条件格式化规则或数据(类型为数字)。忽略范围内没有条件格式规则或数据的任何单元格的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

好的,你有一些错误。该代码不是为处理不存在条件格式的单元格(“CFCELL”)而设计的(只要它们被分配了规则,空白单元格在上述代码中实际上应该是可以的)。

所以,让我们试试这个:

Function CountCFCells(rng As Range, C As Range) As Long
Dim i As Single, j As Long, Total As Long
Dim CFCELL As Range
Dim Match_CI As Long

j = 0
Match_CI = C.Interior.ColorIndex

For Each CFCELL In rng
    If CFCELL.FormatConditions.Count > 0 And Len(CFCELL) > 0 Then
        If DisplayedColor(CFCELL) = Match_CI Then
            j = j + 1
        End If
    End If
Next CFCELL

Total = j
CountCFCells = Total

End Function


Function DisplayedColor(Cell As Range, Optional CellInterior As Boolean = True, _
                        Optional ReturnColorIndex As Long = True) As Long
  Dim X As Long, Test As Boolean, CurrentCell As String
  If Cell.Count > 1 Then Err.Raise vbObjectError - 999, , "Only single cell references allowed for 1st argument."
  CurrentCell = ActiveCell.Address
  For X = 1 To Cell.FormatConditions.Count
    With Cell.FormatConditions(X)
      If .Type = xlCellValue Then
        Select Case .Operator
          Case xlBetween:      Test = Cell.Value >= Evaluate(.Formula1) And Cell.Value <= Evaluate(.Formula2)
          Case xlNotBetween:   Test = Cell.Value <= Evaluate(.Formula1) Or Cell.Value >= Evaluate(.Formula2)
          Case xlEqual:        Test = Evaluate(.Formula1) = Cell.Value
          Case xlNotEqual:     Test = Evaluate(.Formula1) <> Cell.Value
          Case xlGreater:      Test = Cell.Value > Evaluate(.Formula1)
          Case xlLess:         Test = Cell.Value < Evaluate(.Formula1)
          Case xlGreaterEqual: Test = Cell.Value >= Evaluate(.Formula1)
          Case xlLessEqual:    Test = Cell.Value <= Evaluate(.Formula1)
        End Select
      ElseIf .Type = xlExpression Then
        Application.ScreenUpdating = False
        Cell.Select
        Test = Evaluate(.Formula1)
        Range(CurrentCell).Select
        Application.ScreenUpdating = True
      End If
      If Test Then
        If CellInterior Then
          DisplayedColor = IIf(ReturnColorIndex, .Interior.ColorIndex, .Interior.color)
        Else
          DisplayedColor = IIf(ReturnColorIndex, .Font.ColorIndex, .Font.color)
        End If
        Exit Function
      End If
    End With
  Next
  If CellInterior Then
    DisplayedColor = IIf(ReturnColorIndex, Cell.Interior.ColorIndex, Cell.Interior.color)
  Else
    DisplayedColor = IIf(ReturnColorIndex, Cell.Font.ColorIndex, Cell.Font.color)
  End If
End Function

这将计算应用了条件格式规则的所有单元格,其中包含所选范围内的数据。它只匹配与您在公式中选择的第二个单元格颜色相同的条目。在这种情况下,我这样做是因为它将用户输入的零值计为一个值(如果有人实际输入零,则不会跳过它)。