专家请帮忙。以下VBA代码在excel 2010中有效,但在2007年没有。 它显示错误“应用程序或对象未定义”。似乎不支持“selection.FormatConditions.Font”。
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""BREAK TOP"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384 --- Error: application and object undefined
.TintAndShade = 0
End With
提前多多感谢!
答案 0 :(得分:0)
我无法在2007 Excel中对此进行测试,但错误绝对不是FormatConditions.Font
对象。您的.Font.Color
。
在2007年查看.Font
对象的开发参考时,您似乎应该使用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