2016-08-16 91 views
0

如果C5:C10背景色爲紅色,我需要從單元格B5:B10獲取SUM值。Excel - SUMIF下一個單元格顏色爲

因此,可以說C5和C8是紅色的,我需要單元格B5 + B8的SUM。

這些是我使用了模塊:http://www.exceltrick.com/how_to/sum-cells-based-on-background-color/

這不是工作:= SUMIF(C5:C10; 「=」 & GetCellColor(C5:C10)= 「=」 & GetCellColor(A1) ; B5:B10)

在此先感謝!

編輯:

GetCellColor

Function GetCellColor(xlRange As Range) 
Dim indRow, indColumn As Long 
Dim arResults() 

Application.Volatile 

If xlRange Is Nothing Then 
    Set xlRange = Application.ThisCell 
End If 

If xlRange.Count > 1 Then 
    ReDim arResults(1 To xlRange.Rows.Count, 1 To xlRange.Columns.Count) 
    For indRow = 1 To xlRange.Rows.Count 
    For indColumn = 1 To xlRange.Columns.Count 
     arResults(indRow, indColumn) = xlRange(indRow, indColumn).Interior.Color 
    Next 
    Next 
GetCellColor = arResults 
Else 
GetCellColor = xlRange.Interior.Color 
End If 
End Function 
+1

你可以發佈你的UDF? – Brian

+0

我同意。請*編輯*您的問題,**添加GetCellColor()**。 –

回答

0

如果您有任何連續垂直範圍與前一列中的值的顏色這工作,你只是在尋找紅色:

Function CountAdjacent(colorRange As Range) As Long 

Dim clr As Variant 
Dim countValue As Long 

For Each clr In colorRange 
    If clr.Interior.color = vbRed Then 
     countValue = Application.WorksheetFunction.Sum(clr.Offset(0, -1).Value, countValue) 
    End If 
Next clr 
CountAdjacent = countValue 

End Function 

類型=CountAdjacent(yourColorRange)在像任何其他公式的單元格中。

+0

謝謝Brian! – kivi