Instead of creating conditional formatting for a range of cells, I conditionally formatted each cell individually using VBA based on the two subs below. The result is shown in the link below the code. Hope this helps.
' The purpose of this sub is to add a data bar to an individual cell
' The value in the cell is expected to be decimal numbers between -1 and 1
' If the value is greater than or equal to -0.1 and less than or equal to 0.1, then display green bars
' If the value is less than -0.1 and greater than -.2, OR greater than 0.1 and less than 0.2 then yellow bars
' All other scenarios display red bars
Sub Add_Data_Bar(rngCell As Range, dblValue As Double)
' Clears existing conditional formatting from the cell
' Adds a new data bar to the cell
With rngCell.FormatConditions
.Delete
.AddDatabar
End With
' Creates a databar object for the databar that has been added to the cell
Dim dbar As Databar
Set dbar = rngCell.FormatConditions(rngCell.FormatConditions.Count)
' Sets the databar fill type to display as gradient
dbar.BarFillType = xlDataBarFillGradient
' Sets the databar border style
dbar.BarBorder.Type = xlDataBarBorderSolid
' Sets the databar axis position
dbar.AxisPosition = xlDataBarAxisMidpoint
' Sets the minimum limit of the data bar to -1
With dbar.MinPoint
.Modify newtype:=xlConditionValueNumber, newvalue:=-1
End With
' Sets the maximum limit of the data bar to +1
With dbar.MaxPoint
.Modify newtype:=xlConditionValueNumber, newvalue:=1
End With
' Sets the color based on what value has been passed to the sub
' Green
If dblValue <= 0.1 And dblValue >= -0.1 Then
With dbar
.BarColor.Color = RGB(99, 195, 132) ' Green
.BarBorder.Color.Color = RGB(99, 195, 132)
End With
' Yellow
ElseIf (dblValue > 0.1 And dblValue <= 0.2) Or (dblValue < -0.1 And dblValue >= -0.2) Then
With dbar
.BarColor.Color = RGB(255, 182, 40) ' Yellow
.BarBorder.Color.Color = RGB(255, 182, 40)
End With
' Red
Else
With dbar
.BarColor.Color = RGB(255, 0, 0) ' Red
.BarBorder.Color.Color = RGB(255, 0, 0)
End With
End If
End Sub
' Applies the databar formatting to each cell in a range
‘ Call this on the Worksheet_Change event so that the formatting updates when data is refreshed
Sub Loop_Through_Range()
' Range to be looped through
Dim rng As Range
Set rng = Sheet1.Range("A2:A22")
' Range for For Loop
Dim cell As Range
' Loops through each cell in your range
For Each cell In rng.Cells
Call Add_Data_Bar(cell, cell.Value)
Next
End Sub
Worksheet View