2016-11-18 126 views
0

我有一個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 
+0

? '.Formula'屬性在Excel 2013中不起作用。 –

+0

我正在使用Excel 2010. – user2341632

回答

0

既然你已經選擇的細胞補充數據欄進入,您可以使用此範圍內進行添加然後在同一範圍內逐個修改每個條件格式。請注意,.FormatConditions索引不會更改,這是因爲Excel會自動更改索引一旦條件被修改。所以,這個宏只能運行一次。此外,只有當您在選定範圍內有3種現有格式條件時才能使用此功能。

這就是我想出來的。

Public Sub AddDataBars() 
    Dim db As Databar 

    Set db = Selection.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 = Selection.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 = Selection.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 

    With Selection 
     With .FormatConditions(1) 
      If .Operator <> xlLess Then 
       .Modify xlCellValue, xlLess, "0.8" 
      Else 
       .Modify xlCellValue, xlGreater, "0.8" 
       .Modify xlCellValue, xlLess, "0.8" 
      End If 
      .StopIfTrue = False 
     End With 
     With .FormatConditions(1) 
      If .Operator <> xlBetween Then 
       .Modify xlCellValue, xlBetween, "0.8", "1.00001" 
      Else 
       .Modify xlCellValue, xlLess, "1.00001" 
       .Modify xlCellValue, xlBetween, "0.8", "1.00001" 
      End If 
      .StopIfTrue = False 
     End With 
     With .FormatConditions(1) 
      If .Operator <> xlGreater Then 
       .Modify xlCellValue, xlGreater, "1.00001" 
      Else 
       .Modify xlCellValue, xlLess, "1.00001" 
       .Modify xlCellValue, xlGreater, "1.00001" 
      End If 
      .StopIfTrue = False 
     End With 
    End With 
End Sub 

運行宏之前: Before running the macro

運行宏後: 您正在使用哪個版本的Excel After running the macro

+0

非常感謝您的幫助。我不確定我是否理解這一點。我應該在通過代碼添加數據條之前將這3個條件添加到選擇中嗎?因爲,當我在不添加任何條件的情況下運行代碼時,出現錯誤「對象不支持此屬性或方法」。 – user2341632

+0

對不起,延遲迴復。是的,您需要最初添加3個格式條件才能正常工作。當您多次運行該工具時,添加格式條件會產生問題,因爲它會在每次執行時添加3個格式條件,使您的表格真正加載速度很慢。我編輯了我的答案並添加了詳細信息。 –

相關問題