|
VBA 自定义函数
然后表里就可以用这个公式
代码放到VBA的一个模块里,这样整个工作薄都可以用这个函数
- Function color(rg As Range)
- Dim nrow As Integer, ncolumn As Integer, x As Integer, y As Integer
- Dim arr()
- nrow = rg.Rows.Count: ncolumn = rg.Columns.Count
- If nrow = 1 And ncolumn = 1 Then
- color = rg.Interior.ColorIndex
- Else
- ReDim arr(1 To nrow, 1 To ncolumn)
- For x = 1 To nrow
- For y = 1 To ncolumn
- arr(x, y) = rg.Cells(x, y).Interior.ColorIndex
- Next
- Next
- color = arr
- End If
- End Function
复制代码 下面是计算公式
=SUM($A$1:$D$6*(color($A$1:$D$6)=color($A9)))
CTRL+SHIFT+ENTER,数组公式 ,然后下拉OK了
如何实现按背景色求和?.zip
(37.68 KB, 下载次数: 33)
|
|