title: VBA to add databars with three different colors based on a condition
body: <p>I have an excel report where databars have to be added with three different colors (Red, Amber and Green) based on three different conditions. Following code does that perfectly. However, it removes all the conditional formatting that already exists in the selection. When I comment the line Selection.FormatConditions.Delete, it throws an error "Object doesnt support this property or method' at the line c.FormatConditions(1).Formula = "=if(" &amp; cellName &amp; "&lt;0.8, true, false)"</p>
  
  <p>I will need the existing formatting conditions to be retained. Can you please help?</p>
  
  <pre><code>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(" &amp; cellName &amp; "&lt;0.8, true, false)"
  c.FormatConditions(2).Formula = "=if(AND(" &amp; cellName &amp; "&gt;=0.8, " &amp; cellName    &amp; "&lt;=1.00001), true, false)"
  c.FormatConditions(3).Formula = "=if(" &amp; cellName &amp; "&gt;1.00001, true, false)"
  
  Next c
  
  End Sub
  </code></pre>