【问题标题】:Excel summing number of rows over range that adheres to multiple criteriaExcel对符合多个条件的范围内的行数求和
【发布时间】:2014-07-07 02:49:11
【问题描述】:

我正在寻找一种方法来计算我的范围内有多少行(跨越两列)符合以下标准:行中的一个单元格具有特定的背景颜色,而同一行中的另一个单元格具有特定的背景颜色不同的特定背景颜色。

就目前情况而言,我曾尝试在 Countifs 中使用 colorfunction,但他们似乎给出了错误的答案。有什么建议吗?

【问题讨论】:

    标签: excel colors excel-formula array-formulas


    【解决方案1】:

    重启...
    对于两种颜色的检查,对于我的功能,您有:

    Public Function CountColor(Sel As Range, Col As Long, Col1 As Long) As Long
        Application.Volatile
        Dim i As Long
    
        i = 0
        For Each xx In Sel
            If (xx.Interior.Color = Col) And (xx.Offset(0, 1).Interior.Color = Col1) Then i = i + 1
        Next
        CountColor = i
    End Function
    

    如果你想用颜色引用一个单元格:

    Public Function CountColorR(Sel As Range, ColR As Range, ColR1 As Range) As Long
        Application.Volatile
        Dim i, Col, Col1 As Long
    
        Col = ColR.Interior.Color
        Col1 = ColR1.Interior.Color
        i = 0
        For Each xx In Sel
            If (xx.Interior.Color = Col) And (xx.Offset(0, 1).Interior.Color = Col1) Then i = i + 1
        Next
        CountColorR = i
    End Function
    

    在您的函数中,您需要添加代码的 if (...) 和 (...) 部分以及颜色部分...
    在您的函数中,我不明白 SUM 的部分/计数...

    【讨论】:

      【解决方案2】:

      直接使用 EXCEL 函数,我认为这是不可能的,因为您没有返回内部颜色的函数。
      使用 VBA(模块内部):

      Public Function CountColor(Sel As Range, Col As Long) As Long
          Dim i As Long
      
          i = 0
          For Each xx In Sel
              If xx.Interior.Color = Col Then i = i + 1
          Next
          CountColor = i
      End Function
      

      你需要知道颜色...使用函数:

      Public Function InnerColor(Sel As Range) As Long
          InnerColor = Sel.Interior.Color
      End Function
      

      使用名称:定义名称 -> 颜色:

      =GET.CELL(63;OFFSET(INDIRECT("RC";FALSE);0;-1))
      

      你可以有一个间接的参考。不要与 Countif 合作。在这样的情况下:

      使用之前定义的名称,您可以创建一个列:

      =IF(color>0;1;0)
      

      还有一个简单的:

      =SUM(K5:K16)
      

      获得黄色单元格编号。
      如果您不进行重新计算 (F9),请不要立即工作(也包括 VBA)...

      【讨论】:

      • 我认为 excel 2007 中的 colorfunction() 能够告诉您某个范围内单元格的填充颜色是否与指定目标单元格的填充颜色匹配。例如,colorfunction(A1, B1:B100, false) 将计算 B1:B100 范围内有多少单元格的填充颜色与单元格 A1 的填充颜色匹配?
      • 感谢您的回答,但我觉得您好像错过了问题的重点。如上所述使用 colorfunction() ,我可以完全按照您在答案中列出的内容进行操作。我需要计算两列范围内有多少行包含两个阴影的单元格,例如,第 1 列中的一个单元格为蓝色,第 2 列中的另一个为红色。
      • 我没有找到你写的函数...这不是标准...无论如何,VBA可以吗?如果是的话,修改VBA已经写的很简单...
      • 请看下面我的回答
      【解决方案3】:
      Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
      
      Dim rCell As Range
      
      Dim lCol As Long
      
      Dim vResult
      
      
      
      lCol = rColor.Interior.ColorIndex
      
      
      
          If Count = True Then
      
             For Each rCell In rRange
      
              If rCell.Interior.ColorIndex = lCol Then
      
                      vResult = WorksheetFunction.Count(rCell) + vResult
      
              End If
      
             Next rCell
      
          Else
      
              For Each rCell In rRange
      
              If rCell.Interior.ColorIndex = lCol Then
      
                      vResult = 1 + vResult
      
              End If
      
             Next rCell
      
      End If
      
      
      
      ColorFunction = vResult
      
      End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-17
        • 1970-01-01
        相关资源
        最近更新 更多