Set Conditional Formatting in Horizontal and Vertical Ranges
When an application has many rows and columns that require conditional formatting, the code snippet below shows how to create conditional formatting dynamically.
Program Code
' **************************************************************************** ' Remove Previous Conditional Formatting ' **************************************************************************** wksProductPlanning.Cells.FormatConditions.Delete ' **************************************************************************** ' Add Conditional Formatting ' **************************************************************************** For i = 6 To lngLastProductPlanningRow lngStartingInventory = wksProductPlanning.Cells(i, 4) Set rngInventoryLevels = Range(wksProductPlanning.Cells(i, 6), wksProductPlanning.Cells(i, 41)) ' **************************************************************************** ' Red - Inventory Level Below zero ' **************************************************************************** rngInventoryLevels.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:="=0" rngInventoryLevels.FormatConditions(rngInventoryLevels.FormatConditions.Count).SetFirstPriority With rngInventoryLevels.FormatConditions(1).Font .Color = -16383844 .TintAndShade = 0 End With With rngInventoryLevels.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 13551615 .TintAndShade = 0 End With rngInventoryLevels.FormatConditions(1).StopIfTrue = False ' **************************************************************************** ' Yellow - Inventory Level Range Between Zero And Below Safety Stock ' **************************************************************************** strRangeFormula = "=" & wksProductPlanning.Cells(i, 4).Address(RowAbsolute:=True, ColumnAbsolute:=True) rngInventoryLevels.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="=0", Formula2:=strRangeFormula rngInventoryLevels.FormatConditions(rngInventoryLevels.FormatConditions.Count).SetFirstPriority With rngInventoryLevels.FormatConditions(1).Font .Color = -16754788 .TintAndShade = 0 End With With rngInventoryLevels.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 10284031 .TintAndShade = 0 End With rngInventoryLevels.FormatConditions(1).StopIfTrue = False Next i