Excel模块 - VBA:如果满足条件,则计算有色单元格的函数

时间:2017-04-18 12:50:14

标签: excel vba excel-formula

人!

我不经常在Excel中引用VBA,但是当我这样做时,我会通过Googling找到答案。 但是,我目前的需求没有答案。

我有以下功能来计算范围内的颜色(可能的来源 - http://www.ozgrid.com/VBA/sum-count-cells-by-color.htm):

    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.Color


    If SUM = True Then
        For Each rCell In rRange
            If rCell.Interior.Color = lCol Then
                vResult = WorksheetFunction.SUM(rCell)
            End If
        Next rCell
    Else
        For Each rCell In rRange
            If rCell.Interior.Color = lCol Then
                vResult = 1 + vResult
            End If
        Next rCell
    End If

    ColorFunction = vResult
End Function

如果条件满足,我尝试将其扩展到特定范围内的颜色计数,但是失败了。

亲爱的同事们,我请你帮助我扩展功能以满足需求:

计算特定范围内的颜色数量,如果单词" foo"在另一个范围内得到满足

3 个答案:

答案 0 :(得分:1)

根据你的帖子,你想要Count彩色单元格的数量,所以我在你的Function下面进行了修改,以便你发布。

StrCond是第三个参数,Optional也可以检查String中的某个rCell.Value

Function ColorFunction(rColor As Range, rRange As Range, Optional StrCond As String) As Long

    Dim rCell As Range
    Dim lCol As Long
    Dim vResult As Long

    lCol = rColor.Interior.color

    For Each rCell In rRange
        If rCell.Interior.color = lCol Then
            If StrCond <> "" Then
                If rCell.Value = StrCond Then
                    vResult = vResult + 1
                End If
            Else
                vResult = vResult + 1
            End If
        End If
    Next rCell

    ColorFunction = vResult
End Function
测试此Function

屏幕截图单元格值:

enter image description here

在没有第3个参数(按预期获得Function)测试此5后,

屏幕截图的单元格值:

enter image description here

答案 1 :(得分:0)

让我们从这样的事情开始:

Option Explicit

Function ColorFunction(rColor As Range, rRange As Range) As Long

    Dim rCell       As Range
    Dim lCol        As Long
    Dim bFooMet     As Boolean
    Dim lResult     As Long

    lCol = rColor.Interior.Color

    For Each rCell In rRange
        If rCell.Interior.Color = lCol Then lResult = lResult + 1
        If rCell.value = "foo" Then bFooMet = True
    Next rCell

    If bFooMet Then
        ColorFunction = lResult
    Else
        ColorFunction = -1
    End If

End Function

如果您的活动表格如下所示:

enter image description here

并且您在即时窗口中的即时窗口?ColorFunction(cells(1,1),selection)中写入11作为结果 - 黄色的单元格数量与A1具有相同的背景。

如果您在所选范围内没有foo,则会获得-1

如果您只想计算yellow内有foo的单元格,可能会是这样的:

Option Explicit

Function ColorFunction(rColor As Range, rRange As Range) As Long

    Dim rCell       As Range
    Dim lCol        As Long
    Dim bFooMet     As Boolean
    Dim lResult     As Long

    lCol = rColor.Interior.Color

    For Each rCell In rRange
        If rCell.value = "foo" And rCell.Interior.Color = lCol Then
            lResult = lResult + 1
        End If
    Next rCell

    ColorFunction = lResult

End Function

答案 2 :(得分:0)

感谢 Shai Rado 的解决方案,我可以修改脚本,使其占用两个范围:第一个用于所需的彩色单元格,第二个用于所需的单词。脚本是:

    Function ConditionalColorFunction(rColor As Range, rColoredRange As Range, StrCond As String, rCondRange As Range) As Long

    Dim rColoredCell As Range
    Dim lCol As Long
    Dim i As Integer
    Dim iCondRangeColumnsAmount As Integer
    Dim vResult

    lCol = rColor.Interior.Color
    iCondRangeColumnsAmount = rCondRange.Columns.Count

    For Each rColoredCell In rColoredRange
        If rColoredCell.Interior.Color = lCol Then
           For i = 1 To iCondRangeColumnsAmount
               If Cells(rColoredCell.Row, i).Value = StrCond Then
                   vResult = 1 + vResult
                   Exit For
               End If
           Next
        End If
    Next rColoredCell

    ConditionalColorFunction = vResult
End Function

rColor - 具有所需颜色的单元格。

rColoredRange - 范围与不同颜色的单元格。

StrCond - 想要的词。

rCondRange - 范围与不同单词的单元格。