如何使用 vba 在 Excel 2007 中找到条件格式单元格的填充颜色值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7408899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find the fill colour value of a conditionally formatted cell in Excel 2007 using vba?
提问by Kevin C
I am using a colour scale for my conditional formatting in Excel 2007 and I am having a hard time finding out the fill colour code for the conditionally formatted cells. I know Interior.Color returns the default colour value but that does not help when using conditional formatting. I am relay quite surprised at how hard this has been to do.
我在 Excel 2007 中使用色阶进行条件格式设置,但我很难找到条件格式单元格的填充颜色代码。我知道 Interior.Color 返回默认颜色值,但这在使用条件格式时无济于事。我对这件事有多难做到感到非常惊讶。
Thank you.
谢谢你。
回答by aevanko
You can access the interior color of the fomatting conditions (not what the cell currently is) like so, assuming there this is the first condition applied on the cell:
您可以像这样访问格式化条件的内部颜色(不是单元格当前是什么),假设这是应用于单元格的第一个条件:
Range("A1").FormatConditions(1).interior.color
Here's a function that will return the color codes for all the conditional formats a cell contains. It will return nothing if there are no conditions, and if there is a condition but no color is set for it, then it tells you "none".
这是一个函数,它将返回单元格包含的所有条件格式的颜色代码。如果没有条件,它将不返回任何内容,如果有条件但未为其设置颜色,则它会告诉您“无”。
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
UPDATE: In case you are curious (I was), the color code that Excel uses is actually BGR, not RGB. So if you wanted to convert the code to RGB values, you can use this:
更新:如果您好奇(我是),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
回答by Kevin
Hi The answers you have provided did not work because I am using a color scale so it does not return the normal 3 condition values.
嗨,您提供的答案不起作用,因为我使用的是色标,因此它不会返回正常的 3 个条件值。
After much more searching I found one workaround that works. That is to take the data and put it into word then copy it back into excel making the range go to a true colour in the cell so Interior.Color will work. I found someone that has taken and put it into VBA. Here is the linkto it if anyone else is looking to do this.
经过更多的搜索,我找到了一种有效的解决方法。即获取数据并将其放入 word 然后将其复制回 excel 使范围变为单元格中的真实颜色,因此 Interior.Color 将起作用。我发现有人已经把它放到了 VBA 中。如果其他人想要这样做,这里是它的链接。
回答by Rich Harding
I don't have an answer that works with Excel 2007 or lower but from Excel 2010 onwards you can use the following (changing the Range to suit):
我没有适用于 Excel 2007 或更低版本的答案,但从 Excel 2010 开始,您可以使用以下内容(更改范围以适应):
Range("A1").DisplayFormat.Interior.ColorIndex
Fortunately, whilst the software for which I need it is supported on Excel 2003 onwards, I only actually require it in a test procedure, and the test module is removed from the production versions.
幸运的是,虽然我需要它的软件在 Excel 2003 上支持,但我实际上只在测试过程中需要它,并且测试模块从生产版本中删除。
回答by Reafidy
The code below was taken from VBAExpress, all credit too the original author - byundt.
下面的代码取自 VBAExpress,所有的功劳都归功于原作者 - byundt。
It may need to be modified for excel 2007.
可能需要针对 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
回答by AKrag
Easy way: Print screen the spreadsheet. Paste it into paint. Use the pipet tool to find the color. Click Edit color.
简单的方法:打印屏幕电子表格。将其粘贴到油漆中。使用移液器工具查找颜色。单击编辑颜色。
BOOM found your RGB information that you can input back into excel
BOOM 找到了你可以输入回 excel 的 RGB 信息