[英]Conditional Formatting in VBA Macro not working in excel 2007
高手请帮忙。 以下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
在此先多谢!
我无法在2007 Excel中对此进行测试,但该错误绝对不是FormatConditions.Font
对象引起的。 该错误会导致您对.Font.Color的.Font.Color
。
在2007年回顾.Font
对象的dev参考时,似乎应该使用RGB()
公式分配颜色。
http://msdn.microsoft.com/zh-CN/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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.