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

我在Excel 2007中使用了我的条件格式的色阶,而且我很难find有条件格式化的单元格的填充颜色代码。 我知道Interior.Color返回默认的颜色值,但是这并没有帮助,当使用条件格式。 我感到非常惊讶,这是多么的艰难。

谢谢。

假设这是在单元格上应用的第一个条件,您可以访问fomatting条件的内部颜色(不是目前的单元格):

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个条件值。

经过更多的search,我发现了一个解决方法。 那就是把这些数据放到word中,然后把它复制到excel中,使得单元格中的范围变成真正的颜色,这样Interior.Color就可以工作了。 我find了一个已经把它放进VBA的人。 如果有其他人正在这样做,这里是它的链接 。

下面的代码是从VBAExpress中获得的,也都是原作者 – byundt。

可能需要为Excel 2007进行修改。

原始链接

 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 

我没有一个适用于Excel 2007或更低版​​本的答案,但是从Excel 2010开始,您可以使用以下(更改范围以适应):

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

幸运的是,虽然我所需要的软件在Excel 2003以上版本中得到支持,但实际上我只是在testing过程中需要它,而testing模块却从生产版本中删除。

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

BOOM发现你的RGB信息,你可以input回到Excel