如何使用 vba 在 Excel 2007 中找到条件格式单元格的填充颜色值?

Posted

技术标签:

【中文标题】如何使用 vba 在 Excel 2007 中找到条件格式单元格的填充颜色值?【英文标题】:How do I find the fill colour value of a conditionally formatted cell in Excel 2007 using vba? 【发布时间】:2011-11-16 13:23:13 【问题描述】:

我在 Excel 2007 中为我的条件格式使用色标,我很难找出条件格式单元格的填充颜色代码。我知道 Interior.Color 返回默认颜色值,但这在使用条件格式时无济于事。我对这件事的难度感到非常惊讶。

谢谢。

【问题讨论】:

见这里:***.com/questions/996384/… 【参考方案1】:

以下代码取自 VBAExpress,全部归功于原作者 - byundt。

可能需要针对 excel 2007 进行修改。

Original Link

Function ConditionalColor(rg As Range, FormatType As String) As Long
     'Returns the color index (either font or interior) of the first cell in range rg. If no _
    conditional format conditions apply, Then returns the regular color of the cell. _
    FormatType Is either "Font" Or "Interior"
    Dim cel As Range
    Dim tmp As Variant
    Dim boo As Boolean
    Dim frmla As String, frmlaR1C1 As String, frmlaA1 As String
    Dim i As Long

     'Application.Volatile    'This statement required if Conditional Formatting for rg is determined by the _
    value of other cells

    Set cel = rg.Cells(1, 1)
    Select Case Left(LCase(FormatType), 1)
    Case "f" 'Font color
        ConditionalColor = cel.Font.ColorIndex
    Case Else 'Interior or highlight color
        ConditionalColor = cel.Interior.ColorIndex
    End Select

    If cel.FormatConditions.Count > 0 Then
         'On Error Resume Next
        With cel.FormatConditions
            For i = 1 To .Count 'Loop through the three possible format conditions for each cell
                frmla = .Item(i).Formula1
                If Left(frmla, 1) = "=" Then 'If "Formula Is", then evaluate if it is True
                     'Conditional Formatting is interpreted relative to the active cell. _
                    This cause the wrong results If the formula isn 't restated relative to the cell containing the _
                    Conditional Formatting--hence the workaround using ConvertFormula twice In a row. _
                    If the Function were Not called using a worksheet formula, you could just activate the cell instead.
                    frmlaR1C1 = Application.ConvertFormula(frmla, xlA1, xlR1C1, , ActiveCell)
                    frmlaA1 = Application.ConvertFormula(frmlaR1C1, xlR1C1, xlA1, xlAbsolute, cel)
                    boo = Application.Evaluate(frmlaA1)
                Else 'If "Value Is", then identify the type of comparison operator and build comparison formula
                    Select Case .Item(i).Operator
                    Case xlEqual ' = x
                        frmla = cel & "=" & .Item(i).Formula1
                    Case xlNotEqual ' <> x
                        frmla = cel & "<>" & .Item(i).Formula1
                    Case xlBetween 'x <= cel <= y
                        frmla = "AND(" & .Item(i).Formula1 & "<=" & cel & "," & cel & "<=" & .Item(i).Formula2 & ")"
                    Case xlNotBetween 'x > cel or cel > y
                        frmla = "OR(" & .Item(i).Formula1 & ">" & cel & "," & cel & ">" & .Item(i).Formula2 & ")"
                    Case xlLess ' < x
                        frmla = cel & "<" & .Item(i).Formula1
                    Case xlLessEqual ' <= x
                        frmla = cel & "<=" & .Item(i).Formula1
                    Case xlGreater ' > x
                        frmla = cel & ">" & .Item(i).Formula1
                    Case xlGreaterEqual ' >= x
                        frmla = cel & ">=" & .Item(i).Formula1
                    End Select
                    boo = Application.Evaluate(frmla) 'Evaluate the "Value Is" comparison formula
                End If

                If boo Then 'If this Format Condition is satisfied
                    On Error Resume Next
                    Select Case Left(LCase(FormatType), 1)
                    Case "f" 'Font color
                        tmp = .Item(i).Font.ColorIndex
                    Case Else 'Interior or highlight color
                        tmp = .Item(i).Interior.ColorIndex
                    End Select
                    If Err = 0 Then ConditionalColor = tmp
                    Err.Clear
                    On Error GoTo 0
                    Exit For 'Since Format Condition is satisfied, exit the inner loop
                End If
            Next i
        End With
    End If

End Function

【讨论】:

【参考方案2】:

您可以像这样访问格式化条件(不是当前单元格)的内部颜色,假设这是应用于单元格的第一个条件:

Range("A1").FormatConditions(1).interior.color

这是一个函数,它将返回单元格包含的所有条件格式的颜色代码。如果没有条件,它不会返回任何内容,如果有条件但没有为它设置颜色,那么它会告诉你“无”。

Function ConditionalColor(ByVal cell As Range)

Dim colors As String
Dim i As Long

For i = 1 To Range(cell.Address).FormatConditions.count
    If Range(cell.Address).FormatConditions(i).Interior.Color <> 0 Then
        colors = colors & "Condition " & i & ": " & _
        Range(cell.Address).FormatConditions(i).Interior.Color & vbLf
    Else
        colors = colors & "Condition " & i & ": None" & vbLf
    End If
Next

If Len(colors) <> 0 Then
    colors = Left(colors, Len(colors) - 1)
End If

ConditionalColor = colors

End Function

更新: 如果您好奇(我曾经),Excel 使用的颜色代码实际上是 BGR,而不是 RGB。所以如果你想将代码转换为 RGB 值,你可以使用这个:

Function GetRGB(ByVal cell As range) As String

Dim R As String, G As String
Dim B As String, hexColor As String
hexCode = Hex(cell.Interior.Color)

'Note the order excel uses for hex is BGR.
B = Val("&H" & Mid(hexCode, 1, 2))
G = Val("&H" & Mid(hexCode, 3, 2))
R = Val("&H" & Mid(hexCode, 5, 2))

GetRGB = R & ":" & G & ":" & B
End Function

【讨论】:

【参考方案3】:

您好您提供的答案不起作用,因为我使用的是色标,因此它不会返回正常的 3 个条件值。

经过更多搜索,我找到了一种可行的解决方法。也就是说,将数据放入 word 中,然后将其复制回 excel 中,使单元格中的范围变为真实颜色,这样 Interior.Color 就可以工作了。我找到了一个已经把它放入VBA的人。如果其他人想要这样做,这里是link。

【讨论】:

没错,函数If Range(cell.Address).FormatConditions(i).Interior.Color &lt;&gt; 0 Then的第二行生成> error 438 'Object does not support this property or method' > 这是因为我们使用了3个以上的条件。您的解决方法(粘贴到 Word,然后返回 Excel)似乎是最佳选择。 有谁知道从 Excel 粘贴到 Word 时会发生什么。如果我们知道我们可以提取颜色代码。【参考方案4】:

简单的方法: 打印屏幕电子表格。 将其粘贴到油漆中。 使用移液器工具查找颜色。 点击编辑颜色。

BOOM 找到了您可以输入回 excel 的 RGB 信息

【讨论】:

【参考方案5】:

我没有适用于 Excel 2007 或更低版本的答案,但从 Excel 2010 起,您可以使用以下内容(更改范围以适应):

Range("A1").DisplayFormat.Interior.ColorIndex

幸运的是,虽然 Excel 2003 及更高版本支持我需要它的软件,但我实际上只在测试过程中需要它,并且测试模块已从生产版本中删除。

【讨论】:

以上是关于如何使用 vba 在 Excel 2007 中找到条件格式单元格的填充颜色值?的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Excel 2007 VBA 中使用 Option Strict Off 进行后期绑定

excel2007 VBA中如何引用指定的单元格地址?

在 VBA Excel 2007 中使用命名范围

在 excel 2007 vba 中找不到可安装的 ISAM

在 VBA Excel 2007 中传递变体 ByRef

在windows 7 下Excel 2007 VBA中的命令SendKeys有时会无效?