我不能在2007年的Excel中測試這個,但是這個錯誤絕對不是FormatConditions.Font
的對象。該錯誤在.Font.Color
的評價中提高。
回顧2007年.Font
對象的開發參考,似乎應該使用RGB()
公式來分配顏色。
http://msdn.microsoft.com/en-us/library/office/bb213182(v=office.12).aspx
然而,我的谷歌賦表明您不能使用負的顏色值與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
UPDATE
試試這個,不要使用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
這[在Excel 2007被支撐](http://msdn.microsoft.com/en-us/library/office/bb223827(V = office.12)的.aspx)根據開發參考。我在2010年測試代碼,它正在工作。無論如何,如果'.Font'是一個問題,錯誤將發生在'With Selection.FormatConditions(1).Font'行,而不是'.Color'語句。 –
@DavidZemens謝謝!如果註釋掉「.Color」和「.TintAndShade」,程序可以無誤地運行。我已經更新了Microsoft的兼容包,但仍然無濟於事。友善的建議! – rpg
我認爲你需要在2007年使用'RGB'顏色分配。這不適用於負值的顏色值。我會發布更詳細的答案。 –