【发布时间】:2018-02-19 17:30:50
【问题描述】:
如果单元格是某种颜色,但我想加起来的值是偏移的,我想加起来。例如,如果我正在寻找黄色单元格并且 A1 和 A2 是黄色的,那么将 B1 和 B2 加在一起。用户应该能够选择颜色。我花了几个小时试图弄清楚如何做到这一点,我对 VBA 很陌生。任何帮助将非常感激。
Function SumByColor(CellColor As Range, rRange As Range)
Dim ColIndex As Integer
Dim total As Long
Dim cell As Range
ColIndex = CellColor.Interior.ColorIndex
For Each cell In rRange
If cell.Interior.ColorIndex = ColIndex Then
total = total + cell.Offset(0, -19).Value 'adds all the values in range with offset of 0,-19
End If
Next cell
SumByColor = total
End Function
【问题讨论】:
-
这个颜色是通过条件格式设置的吗?如果是,你需要
.DisplayFormat.Interior.Color。 -
你没有提到到底是什么问题。手足无措,莫非有时候
cell.offset(0,-19)会报错?如果是,一个简单的错误检查 (Cell.Column >19) 应该可以解决问题