按ALT+F11,调出VBA窗口,在左上角的工程窗体空白处点击右键——插入——模块,左边窗口中复制以下内容:
Function SumColor(rColor As Range, rSumRange As Range)
Application.Volatile
iCol = rColor.Interior.ColorIndex
For Each rCell In rSumRange
If rCell.Interior.ColorIndex = iCol Then
vResult = WorksheetFunction.Sum(rCell) + vResult
End If
Next rCell
SumColor = vResultEnd Function
关闭VBA窗口回到excel,假设你的数据在A列,B1输入
=sumcolor(A1,A1:A15)
计算结果是A1:A15中颜色为A1单元格底色的数据之和。