Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在VBA中设置背景色,以便在公式中检测它?_Excel_Vba - Fatal编程技术网

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

Excel 如何在VBA中设置背景色,以便在公式中检测它?,excel,vba,Excel,Vba,我使用以下代码将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")

我使用以下代码将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
Function ColorFunction(颜色作为范围,排列作为范围,可选StrCond作为字符串)长度为
变暗rCell As范围
暗淡的lCol尽可能长
暗淡的结果就像长的一样
lCol=rColor.Interior.Color
对于安排中的每个rCell
如果rCell.Interior.Color=lCol,则
如果StrCond“”则
如果rCell.Value=StrCond,则
vResult=vResult+1
如果结束
其他的
vResult=vResult+1
如果结束
如果结束
下一个rCell
ColorFunction=vResult
端函数

如果使用公式为具有条件格式的单元格着色,以确定要格式化的单元格,此UDF将计算具有该颜色的单元格数(改编自)

通常,您可以使用范围的
DisplayFormat.Interior.color
,而不是其
Interior.color
,来确定条件格式应用的颜色。如果您修改了
ColorFunction
以查看
rCell.DisplayFormat.Interior.Color
,并在
中使用了该函数,即类似于这样的函数,它将正常工作

Sub Test
    Range("A2").Value = ColorFunction(Range("A1"), Range("B17:B20"))
End Sub
但是,
ColorFunction
将不能作为自定义项工作-您的
#值注释中提到的错误。我不完全确定原因,但显然使用
Range.DisplayFormat
不能与UDF一起工作。因此,要有一个UDF,您需要遍历每个范围,并计算与条件格式关联的公式

这种方法有两个细微的缺陷:

<> LI>如果存在多个.< /LI>,您需要确定<>代码>范围> <代码> > >
  • 与“right”
    FormatCondition
    -在原始情况下
    =COUNTIF(B$17:B17,B17)>1
    -关联的公式不会自动更新相关引用(如果有)。您需要使用
    Application.ConvertFormula
    ,从A1转换为R1C1(并返回A1)引用样式,同时使用RelativeTo参数更新相对引用


  • 屏幕截图


    这将无法检测CF格式。使用CF条件,或者有一个VBA替代方案-从内存中,它类似于displayformat。如果处理条件格式,则必须使用
    范围.displayformat.Interior.Color
    。我不确定我将在何处插入您提到的代码您是否在工作表上使用
    ColorFunction
    ,或者使用VBA来调用它?@Drummo2a将
    lCol=rColor.Interior.Color
    更改为
    lCol=rColor.DisplayFormat.Interior.Color
    rCell.Interior.Color=lCol
    似乎可行,但我需要更好的方法。我检查重复项的范围超过700000行(1列)。这要花很长时间。@Drummo2a这是我的初始点-条件格式不是最好的方法,因为您必须为每个单元格计算公式。要么以不同的方式给单元格上色(现在我看这似乎是你的问题),要么使用公式来计算重复数-这似乎是你已经尝试过的。@Drummo2a如果你修改
    ColorFunction
    来处理
    Range.DisplayFormat.Interior.color
    ,可能会更快,但是,不要将其用作自定义项,而是在
    Sub
    中使用它,并将输出写入所需的单元格。请原谅,我是新手,我如何使用Sub来完成此操作。@Drummo2a-请参阅我的最新编辑。基本上如前所述,修改原始的
    ColorFunction
    以查看
    rCell.DisplayFormat.Interior.Color
    ,但不要将其用作自定义项,而是在
    Sub
    中使用它并将输出写入单元格。
    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