是否可以根据一列单元格的背景颜色 increment/decrement 一个单元格值?
Is it possible to increment/decrement one cellvalue based on the background-color of a column of cells?
例如,我有一列 10 个单元格,其中 4 个具有绿色背景色,其他具有 none。 (不一定按顺序)
是否可以让一个单元格来计算该列中具有绿色背景颜色的单元格的数量?如果无法计数,是否可以根据背景颜色递增或递减?
使用我的示例,这意味着因为有 4 个单元格具有绿色背景颜色,所以不同的单元格将具有 4 的值,如果我想计算非彩色单元格,则为 6。 (没有功能的例子见图片)
我已经尝试过 IF 语句或尝试使用条件规则,但我还没有一个可用的函数。
有人知道怎么做吗?
Excel color counting
您必须创建一个 VBA 函数。这将做到:
Function CellColor(Target As Range) As Long
' gives the color of the target cell
' ranges from 0 (black) in hex to FFFFFF (white or no color)
' the hex range is BBGGRR (B = blue, G = green and R = red)
' example, bright red would be 0000FF or 255
CellColor = Target.Interior.Color
End Function
这将像这样使用:
数组函数
要计算范围内某种颜色的单元格数量,您需要一个 VBA UDF,它是 returns 一个数组。你可以使用这个版本:
Function CellColors(Target As Range) As Variant()
Dim resultArr() As Variant
'Create a collection
Dim col As New Collection
'Adding values to collection
On Error Resume Next
For Each v In Target
col.Add v.Interior.Color
Next
On Error GoTo 0
'completing operation on each value and adding them to Array
ReDim resultArr(col.Count - 1, 0)
For i = 0 To col.Count - 1
resultArr(i, 0) = col(i + 1)
Next
CellColors = resultArr
End Function
然后你可以在一个范围内应用它,像这样:
= SUM( --(CellColors(A1:A10)<16777215) )
这个公式只是说“计算 non-white 个细胞”。
例如,我有一列 10 个单元格,其中 4 个具有绿色背景色,其他具有 none。 (不一定按顺序)
是否可以让一个单元格来计算该列中具有绿色背景颜色的单元格的数量?如果无法计数,是否可以根据背景颜色递增或递减? 使用我的示例,这意味着因为有 4 个单元格具有绿色背景颜色,所以不同的单元格将具有 4 的值,如果我想计算非彩色单元格,则为 6。 (没有功能的例子见图片)
我已经尝试过 IF 语句或尝试使用条件规则,但我还没有一个可用的函数。
有人知道怎么做吗?
Excel color counting
您必须创建一个 VBA 函数。这将做到:
Function CellColor(Target As Range) As Long
' gives the color of the target cell
' ranges from 0 (black) in hex to FFFFFF (white or no color)
' the hex range is BBGGRR (B = blue, G = green and R = red)
' example, bright red would be 0000FF or 255
CellColor = Target.Interior.Color
End Function
这将像这样使用:
数组函数 要计算范围内某种颜色的单元格数量,您需要一个 VBA UDF,它是 returns 一个数组。你可以使用这个版本:
Function CellColors(Target As Range) As Variant()
Dim resultArr() As Variant
'Create a collection
Dim col As New Collection
'Adding values to collection
On Error Resume Next
For Each v In Target
col.Add v.Interior.Color
Next
On Error GoTo 0
'completing operation on each value and adding them to Array
ReDim resultArr(col.Count - 1, 0)
For i = 0 To col.Count - 1
resultArr(i, 0) = col(i + 1)
Next
CellColors = resultArr
End Function
然后你可以在一个范围内应用它,像这样:
= SUM( --(CellColors(A1:A10)<16777215) )
这个公式只是说“计算 non-white 个细胞”。