【发布时间】:2020-10-24 09:18:19
【问题描述】:
我有一张附有许多切片器的桌子。该表格具有条件格式,可根据特定条件将数字列的字体更改为红色或绿色。
我见过许多基于背景颜色计算单元格的函数,但我想做的是;
- 仅小计范围内与单元格“R2”字体颜色相同的可见单元格
- 将在单元格“R2”中返回总和
- 范围是“M9:M200”
- 然后我需要通过切片器更改运行此操作,因为更新切片器会更改所有数字。
我在这里找到了一些我试图适应但失败的代码。任何帮助表示赞赏
我一直在尝试使用以下代码将条件格式(红色单元格)转换为普通红色,但这也不起作用。
Sub GetColorSum2()
Dim FCELL As Range
For Each rCell In Range("Y1:Y7").Cells
If rCell.DisplayFormat.Interior.ColorIndex = 24 Then
rCell.Font.ColorIndex = 3
End If
Next
End Sub
主要代码
Function GetColorsum(sumRange As Range, SumColor As Range, Optional VolatileParameter As Variant)
Dim ColVal As Long, rCell As Range
Dim Totalsum As Long
ColVal = SumColor.Font.ColorIndex
For Each rCell In sumRange.Cells
If rCell.Font.ColorIndex = ColVal Then
If rCell.EntireRow.Hidden = False Then
If rCell.EntireColumn.Hidden = False Then
Totalsum = Totalsum + 1
End If
End If
End If
Next rCell
GetColorsum = Totalsum
End Function
我也试过
Function Sumclr(rColor As Range, rRange As Range)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.Sum(rCell, vResult)
End If
Next rCell
Sumclr = vResult
End Function
【问题讨论】:
-
使用 CF 产生的颜色只能通过
DisplayFormat属性(例如myCell.DisplayFormat.Interior.ColorIndex)获得,但如果您在这里尝试使用 UDF,那么不幸的是,DisplayFormat在UDF。