如何在VBA中设置背景颜色,以便在公式中检测到它?

问题描述 投票:3回答:1

我使用以下代码在B列中将重复值设置为红色。我不记得它来自何处。我添加了计数器和msgbox,看看发生了什么。 B列中的重复值突出显示为红色。

Sub formatduplicates2()
Dim rg As Range
Dim cf As FormatCondition
Dim datax As Range
Dim xcolor As Long
Dim colorcount
Dim count

colorcount = 1
Set rg = Range("B17", Range("B17").End(xlDown))
Set cf = rg.FormatConditions.Add(Type:=xlExpression, Formula1:="=COUNTIF(B$17:B17,B17)>1")
cf.Interior.Color = RGB(255, 0, 0)
For Each datax In rg
    count = count + 1
    If count = 10000 Then
        MsgBox count
    End If
Next datax

End Sub

我使用以下函数来计算所有红细胞。这是来自Ozgrid。如果我手动更改单元格的背景颜色,它可以工作。

使用上述代码更改的单元格不会被该函数看到。当我手动检查B列中单元格的背景颜色(显然是红色的)时,Excel告诉我RGB设置为0,0,0(无填充)。重新计算,保存和重新打开文件的数量不会改变任何内容。单元格看起来是红色的,但与之关联的数据是“无填充”。

我的目标是计算给定背景的细胞。

Function ColorFunction(rColor As Range, rRange As Range, Optional StrCond As String) As Long

    Dim rCell As Range
    Dim lCol As Long
    Dim vResult As Long

    lCol = rColor.Interior.Color

    For Each rCell In rRange
        If rCell.Interior.Color = lCol Then
            If StrCond <> "" Then
                If rCell.Value = StrCond Then
                    vResult = vResult + 1
                End If
            Else
                vResult = vResult + 1
            End If
        End If
    Next rCell

    ColorFunction = vResult
End Function
excel vba
1个回答
0
投票

如果使用公式确定要格式化的单元格,则使用条件格式对单元格进行着色,此UDF将计算具有该颜色的单元格数(从Microsoft Community改编)。

您通常可以通过使用范围的DisplayFormat.Interior.Color来确定条件格式应用的颜色,而不是Interior.Color。如果你修改你的ColorFunction来查看rCell.DisplayFormat.Interior.Color并在Sub中使用该函数,即这样的东西,它会正常工作。

Sub Test
    Range("A2").Value = ColorFunction(Range("A1"), Range("B17:B20"))
End Sub

但是ColorFunction不能作为UDF工作 - 你在评论中提到的#Value!错误。我不完全确定原因,但显然使用Range.DisplayFormat不适用于UDF。因此,要拥有UDF,您需要遍历每个范围并评估与条件格式关联的公式。

这种方法有两个轻微的皱纹:

  1. 你需要确定哪个FormatCondition你要考虑Range,如果有多个。
  2. 与“正确”FormatCondition相关联的公式 - 在您的原始情况下=COUNTIF(B$17:B17,B17)>1 - 将不会自动更新相对引用(如果有的话)。您需要使用Application.ConvertFormula,从A1转换为R1C1(并返回A1)参考样式,同时使用RelativeTo参数更新相对引用。

Function CountConditionColorCells(CellsRange As Range, ColorRng As Range) As Long
    Dim RightCF As Boolean
    Dim CFformula As String
    Dim CFCELL As Range
    Dim CFcounter, CFtrueCounter As Long, CFCellCounter As Long

    ' Determines which conditional format to consider, if multiple
    For CFcounter = 1 To CellsRange.FormatConditions.Count
        If CellsRange.FormatConditions(CFcounter).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
            RightCF = True
            Exit For
        End If
    Next CFcounter

    If RightCF Then
        For Each CFCELL In CellsRange
            CFformula = CFCELL.FormatConditions(CFcounter).Formula1
            CFformula = Application.ConvertFormula(CFformula, xlA1, xlR1C1)
            CFformula = Application.ConvertFormula(CFformula, xlR1C1, xlA1, , _
                ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CFCellCounter + 1))

            If Evaluate(CFformula) Then CFtrueCounter = CFtrueCounter + 1

            CFCellCounter = CFCellCounter + 1
        Next CFCELL
    Else
        Exit Function ' Returns 0
    End If

    CountConditionColorCells = CFtrueCounter
End Function

截图

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.