我无法在 2007 Excel 中对此进行测试,但该错误绝对与FormatConditions.Font
对象无关。您对.Font.Color
.
回顾.Font
2007 年对象的开发参考,您似乎应该使用RGB()
公式来分配颜色。
http://msdn.microsoft.com/en-us/library/office/bb213182(v=office.12).aspx
但是,我的 Google-Fu 表明您不能RBG
在分配中使用负颜色值。我想你需要选择不同的颜色。也许有一些使用 WinAPI 的方法,但我目前无法测试这种方法。
我选择了另一种与你的色调相似的蓝色-16752384
。
Sub test()
'## This section converts a long color to its R/G/B components
Dim col As Long: col = 15773696
Dim r As Long, g As Long, b As Long
r = col Mod 256
g = (col \ 256) Mod 256
b = (col \ 256 \ 256) Mod 256
'## Your code, with modification for the RGB Formula:
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""BREAK TOP"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = RGB(r, g, b) '<-- RGB Formula, here.
.TintAndShade = 0
End With
End Sub
更新
试试这个,不要使用Selection
对象。
Sub test2()
Dim rng as Range
Dim fc as FormatCondition
Set rng = Range(Selection.Address)
'## This section converts a long color to its R/G/B components
Dim col As Long: col = 15773696
Dim r As Long, g As Long, b As Long
r = col Mod 256
g = (col \ 256) Mod 256
b = (col \ 256 \ 256) Mod 256
'## Your code, with modification for the RGB Formula:
rng.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""BREAK TOP"""
rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
Set fc = rng.FormatConditions(1)
fc.Font.Color = RGB(r, g, b) '<-- RGB Formula, here.
fc.Font.TintAndShade = 0
End Sub