Excel赢得';使用sumbycolor功能时无法识别内饰颜色
嘿,伙计们,我需要帮助。我编写的代码有条件地格式化包含特定数字的单元格,然后将彩色单元格排序到底部,最后我不想对只包含内部颜色的单元格求和。问题是,当我执行函数sumbycolor excel时,无法识别单元格的颜色并对整个列求和(颜色或无颜色)。我该如何解决这个问题?谢谢大家!Excel赢得';使用sumbycolor功能时无法识别内饰颜色,excel,vba,colors,sum,excel-formula,Excel,Vba,Colors,Sum,Excel Formula,嘿,伙计们,我需要帮助。我编写的代码有条件地格式化包含特定数字的单元格,然后将彩色单元格排序到底部,最后我不想对只包含内部颜色的单元格求和。问题是,当我执行函数sumbycolor excel时,无法识别单元格的颜色并对整个列求和(颜色或无颜色)。我该如何解决这个问题?谢谢大家! Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean) Dim rCell As Range Dim lCol
Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function
Sub MySheet
'Conditional formatting
Range("A1").CurrentRegion.Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$F1=99999"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
'Sort by color column C
Dim sht As Worksheet
Dim rngSort As Range
Dim rngTable As Range
Set sht = ActiveSheet
RowCount = sht.Range("A1").End(xlDown).Row
Set rngSort = sht.Range("A1:A" & RowCount)
Set rngTable = Range(Range("A1"), ActiveCell.SpecialCells(xlLastCell))
sht.Sort.SortFields.Clear
sht.Sort.SortFields.Add(rngSort, _
xlSortOnCellColor, xlDescending, , _
xlSortNormal).SortOnValue.Color = RGB(255, 255, 0)
With sht.Sort
.SetRange rngTable
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Sum by color
lr = Cells(Rows.Count, "O").End(xlUp).Row - 1
sumRange = Range("O2:O" & lr).Address
lr = Cells(Rows.Count, "P").End(xlUp).Row - 1
sumRange2 = Range("P2:P" & lr).Address
lr = Range("B" & Rows.Count).End(xlUp).Row
CellColor = Range("B" & lr).Address
Range("C1").Select
ActiveCell.End(xlDown).Offset(9, 0).Formula = "=(ColorFunction(" & CellColor & "," & sumRange & ",TRUE)+ColorFunction(" & CellColor & "," & sumRange2 & ",TRUE))"
End Sub
Excel的条件格式不会更改单元格本身的各种颜色属性(例如Interior.color、Interior.ColorIndex、Font.color等)。为了处理这个问题,您需要使用实际的条件格式公式,并测试单元格以查看哪一个(如果有的话)是适用的 很容易编写一个简短的例程,您可以控制颜色和条件
对于通用例程,您可以尝试EJ Gunderson例程。我看不到您的SumByColor函数。条件格式设置不会更改.interior.color属性。您需要检查单元格的条件格式条件,以便对其进行计数。如果你的功能不这样做,它将不会像你期望的那样工作。请原谅我。我在上面添加了颜色函数。它不适用于条件格式应用的颜色我是否简单地将rCell.Interior.ColorIndex=lCol更改为rCell.Interior.ColorIndex=65535?不,正如我在上面所写的,CF不会更改.Interior.ColorIndex属性。您需要检查每个单元格的条件格式化条件,而不是颜色本身