我在Excel2007中使用色标进行条件格式设置,并且很难找到条件格式单元格的填充颜色代码。我知道Interior.Color返回默认的颜色值,但这在使用条件格式时没有帮助。我对这件事的艰难程度感到非常惊讶。
谢谢。
发布于 2011-09-14 01:44:38
您可以像这样访问格式化条件的内部颜色(而不是当前单元格的颜色),假设这是应用于单元格的第一个条件:
Range("A1").FormatConditions(1).interior.color
这里有一个函数,它将返回一个单元格包含的所有条件格式的颜色代码。如果没有条件,它将不返回任何内容,如果有条件,但没有为其设置颜色,则它会告诉您"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
更新:如果你很好奇(我也是),你会发现它使用的颜色代码实际上是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
发布于 2011-09-20 20:05:14
您好,您提供的答案不起作用,因为我使用的是色标,所以它不会返回正常的3个条件值。
经过更多的搜索,我找到了一个可行的解决方法。也就是说,将数据放入word中,然后将其复制回excel中,使单元格中的范围变为真实颜色,这样Interior.Color就可以工作了。我找到了一个人,把它放到了VBA中。如果其他人想要这样做,这里是它的link。
发布于 2015-11-19 19:12:28
我没有适用于Excel 2007或更低版本的答案,但从Excel 2010开始,您可以使用以下方法(根据需要更改范围):
Range("A1").DisplayFormat.Interior.ColorIndex
幸运的是,虽然我所需要的软件在Excel2003和更高版本上都得到了支持,但我实际上只在测试过程中需要它,并且测试模块已从生产版本中删除。
https://stackoverflow.com/questions/7408899
复制