0

请高手帮忙。以下 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

提前非常感谢!

4

1 回答 1

0

我无法在 2007 Excel 中对此进行测试,但该错误绝对与FormatConditions.Font对象无关。您对.Font.Color.

回顾.Font2007 年对象的开发参考,您似乎应该使用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
于 2013-07-10T15:38:49.223 回答