根据颜色和日期求和单元格

时间:2013-05-09 14:27:12

标签: excel date colors sum

我有以下内容:

日期 ------- 费用

Jan      £500
Jan      £600
Feb      £300
Feb      £600
March    £1000
March    £500

成本单元的颜色取决于其当前状态(已确认绿色,未经证实的白色,半确认黄色),我需要一个公式来计算所有成本,例如绿色和二月份。

我知道这种颜色函数需要VBA,并且找到了一个名为colorfunction()的有用函数,它允许我使用以下公式对颜色单元格求和/计数:

colorfunction(A1, B1:B5, FALSE)

A1是比较范围的颜色,FALSE / TRUE返回总和或计数结果。

但是我无法将此自定义函数用于MONTH()公式或SUMIF。 我可能会完全过度复杂化,所以请指出我试图解决的任何愚蠢的错误。

2 个答案:

答案 0 :(得分:1)

将此函数添加到VBA模块以返回单元格内部颜色索引:

    Function ColorIndex(rng As Range, _
                    Optional text As Boolean = False) As Variant
    Dim cell As Range, row As Range
    Dim i As Long, j As Long
    Dim iWhite As Long, iBlack As Long
    Dim aryColours As Variant

    If rng.Areas.Count > 1 Then
        ColorIndex = CVErr(xlErrValue)
        Exit Function
    End If

    iWhite = WhiteColorindex(rng.Worksheet.Parent)
    iBlack = BlackColorindex(rng.Worksheet.Parent)

    If rng.Cells.Count = 1 Then
        If text Then
            aryColours = DecodeColorIndex(rng, True, iBlack)
        Else
            aryColours = DecodeColorIndex(rng, False, iWhite)
        End If

    Else
        aryColours = rng.Value
        i = 0

        For Each row In rng.Rows
            i = i + 1
            j = 0

            For Each cell In row.Cells
                j = j + 1

                If text Then
                    aryColours(i, j) = _
                      DecodeColorIndex(cell, True, iBlack)
                Else
                    aryColours(i, j) = _
                      DecodeColorIndex(cell, False, iWhite)
                End If

            Next cell

        Next row

    End If

    ColorIndex = aryColours

End Function


Private Function WhiteColorindex(oWB As Workbook)

Dim iPalette As Long
    WhiteColorindex = 0
    For iPalette = 1 To 56
        If oWB.Colors(iPalette) = &HFFFFFF Then
            WhiteColorindex = iPalette
            Exit Function
        End If
    Next iPalette
End Function

Private Function BlackColorindex(oWB As Workbook)
Dim iPalette As Long
    BlackColorindex = 0
    For iPalette = 1 To 56
        If oWB.Colors(iPalette) = &H0 Then
            BlackColorindex = iPalette
            Exit Function
        End If
    Next iPalette
End Function

Private Function DecodeColorIndex(rng As Range, _
                                  text As Boolean, _
                                  idx As Long)
Dim iColor As Long
    If text Then
        iColor = rng.Font.ColorIndex
    Else
        iColor = rng.Interior.ColorIndex
    End If
    If iColor < 0 Then
        iColor = idx
    End If
    DecodeColorIndex = iColor
End Function

然后得到颜色指数为14(绿色)的所有细胞的计数使用sumproduct如下:

= SUMPRODUCT( - (ColorIndex(B1:B100000)= 14),B1:B100000)

这将返回范围B1中所有单元格的总和:B100000,颜色为14(绿色)

最终的例子应该是这样的:

ColorIndex

此外,如果您更喜欢Sumifs而不是Sumproduct,yopu可以选择使用辅助列。在费用旁边的列中输入=ColorIndex(B1),然后向下拖动

enter image description here

然后在另一个单元格中输入公式

=SUM(SUMIFS(B1:B10,C1:C10,14,A1:A10,{"FEB","MARCH"}))

将月份替换为您想要总和的月份(您的oringal标题表示这是您的最终目标)。

这将汇总帮助行表示索引为14(绿色)且月份为2月或3月的成本值

enter image description here

答案 1 :(得分:0)

我意识到你要求编程答案,我不知道你的项目范围。

这是一个简单的解决方案,无需编程。

按颜色过滤,选择单元格,总和将显示在底部(即2100)

Screen shot

相关问题