我有一個Excel報告,其中數據欄必須根據三種不同的條件添加三種不同的顏色(紅色,琥珀色和綠色)。以下代碼完美無缺。但是,它將刪除選區中已存在的所有條件格式。當我註釋行Selection.FormatConditions.Delete,它會在行c.FormatConditions(1).Formula =「= if(」& cellName &「< 0.8,true,則引發錯誤」對象不支持此屬性或方法「 FALSE)」VBA根據條件添加三種不同顏色的數據欄
我需要在現有格式的條件予以保留。能否請您幫忙嗎?
Public Sub AddDataBars()
Selection.FormatConditions.Delete
For Each c In Selection
Dim db As Databar
Set db = c.FormatConditions.AddDatabar
db.ShowValue = True
db.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
db.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=1
db.BarColor.Color = RGB(0, 255, 0)
db.BarColor.TintAndShade = 0
db.BarFillType = xlDataBarFillGradient
db.Direction = xlContext
db.NegativeBarFormat.ColorType = xlDataBarColor
db.BarBorder.Type = xlDataBarBorderNone
db.AxisPosition = xlDataBarAxisAutomatic
db.AxisColor.Color = 0
db.AxisColor.TintAndShade = 0
db.NegativeBarFormat.Color.Color = 255
db.NegativeBarFormat.Color.TintAndShade = 0
Set db = c.FormatConditions.AddDatabar
db.ShowValue = True
db.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
db.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=1
db.BarColor.Color = RGB(255, 192, 0)
db.BarColor.TintAndShade = 0
db.BarFillType = xlDataBarFillGradient
db.Direction = xlContext
db.NegativeBarFormat.ColorType = xlDataBarColor
db.BarBorder.Type = xlDataBarBorderNone
db.AxisPosition = xlDataBarAxisAutomatic
db.AxisColor.Color = 0
db.AxisColor.TintAndShade = 0
db.NegativeBarFormat.Color.Color = 255
db.NegativeBarFormat.Color.TintAndShade = 0
Set db = c.FormatConditions.AddDatabar
db.ShowValue = True
db.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
db.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=1
db.BarColor.Color = RGB(255, 130, 130)
db.BarColor.TintAndShade = 0
db.BarFillType = xlDataBarFillGradient
db.Direction = xlContext
db.NegativeBarFormat.ColorType = xlDataBarColor
db.BarBorder.Type = xlDataBarBorderNone
db.AxisPosition = xlDataBarAxisAutomatic
db.AxisColor.Color = 0
db.AxisColor.TintAndShade = 0
db.NegativeBarFormat.Color.Color = 255
db.NegativeBarFormat.Color.TintAndShade = 0
cellName = c.Address
c.FormatConditions(1).Formula = "=if(" & cellName & "<0.8, true, false)"
c.FormatConditions(2).Formula = "=if(AND(" & cellName & ">=0.8, " & cellName & "<=1.00001), true, false)"
c.FormatConditions(3).Formula = "=if(" & cellName & ">1.00001, true, false)"
Next c
End Sub
? '.Formula'屬性在Excel 2013中不起作用。 –
我正在使用Excel 2010. – user2341632