【问题标题】:Excel VBA module does not ignore blank cellsExcel VBA 模块不会忽略空白单元格
【发布时间】:2015-12-27 09:06:51
【问题描述】:

我有一个 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) 调用此函数时,我希望它忽略任何没有任何条件格式规则或数据(类型为数字)的单元格。忽略范围内没有条件格式规则或数据的任何单元格的最佳方法是什么?

【问题讨论】:

  • 到目前为止您尝试过什么?你问最好的办法,有没有想出办法?
  • 不,到目前为止我还没有解决方案。只要应用了条件格式规则,即使单元格为黑色,该代码也可以工作。但是,我希望它忽略没有应用条件格式规则的单元格。

标签: excel vba


【解决方案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

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

【讨论】:

  • 我希望代码能够处理没有条件格式的单元格。我有不同长度的列,每个包含数据的单元格都有 3 个条件格式规则。每条规则都会改变单元格的颜色。我使用的代码计算了(例如)紫色、绿色和蓝色单元格的数量。所以,你在这里做一些事情,但实际上我仍然想输入一个范围和一个与我可以在范围中查找的条件格式匹配的单元格。由此,我想要紫色(或绿色或蓝色)细胞的计数。而且,当范围包含没有条件格式的单元格时,我不希望出现错误。
  • "空白单元格在上面的代码中实际上应该没问题,只要它们被分配了规则" - 这是真的
  • 我重写的代码已经可以处理没有条件格式规则的单元格(它会跳过它们)。我已经重新添加了第二个参数,它只会像原来一样匹配选定的颜色。
  • 这段代码不起作用,我认为它不起作用,因为计算颜色填充与计算条件格式产生的颜色填充不同。如果您有一个带有白色填充的单元格,值为 8 和一个条件格式规则,如果值 > 10 则将颜色填充更改为红色,则单元格颜色填充将为红色,但实际颜色填充仍为白色。
  • 条件格式很棘手 - 代码不起作用,因为事实证明 .DisplayFormat 实际上不能在函数中使用。查看我的更新回复。
猜你喜欢
  • 2018-11-20
  • 2011-04-19
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多