简体   繁体   English

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

[英]Excel VBA module does not ignore blank cells

I have a VBA module I got online to count cell with conditional formatting. 我有一个VBA模块,可以上网进行条件格式计数。 This module has an issue in that it returns an error if in the range it is counting, a cell is either blank or does not have a conditional format rule. 该模块的问题在于,如果在计数范围内某个单元格为空白或没有条件格式规则,则它将返回错误。 The module is: 该模块是:

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

When I call this function using =CountCFCells(A1:A30, B1) , I want it to ignore any cells don't have any conditional formating rules or data (type is number). 当我使用=CountCFCells(A1:A30, B1)调用此函数时,我希望它忽略任何没有任何条件格式设置规则或数据(类型为数字)的单元格。 What is the best way to disregard any cells in the range that do not have conditional formatting rules or data? 忽略范围中没有条件格式设置规则或数据的任何单元格的最佳方法是什么?

Okay you have a few errors. 好的,您有一些错误。 The code isn't designed to handle a cell ("CFCELL") where conditional formatting is nonexistent (blank cells should actually be okay in the above code so long as they're assigned a rule). 该代码不是为处理不存在条件格式设置的单元格(“ CFCELL”)而设计的(在上面的代码中,只要为空单元格分配了规则,它们实际上就可以了)。

So, let's try this instead: 因此,让我们尝试以下方法:

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

This will count all cells which have conditional formatting rules applied to them containing data within the selected range. 这将计算所有已应用条件格式设置规则的单元格,其中包含选定范围内的数据。 It will only match entries which are the same color as cell you select second in the formula. 它只会匹配与您在公式中选择第二个单元格相同颜色的条目。 In this case, I made it so it counts user-entered zero's as a value (will not skip it if someone actually entered a zero). 在这种情况下,我做到了,因此它将用户输入的零作为一个值进行计数(如果有人实际输入了零,则不会跳过它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM