I am trying to use VBA to do conditional formatting on a similar range. I am sure that the error in my code has something to do with the precedence but I cannot figure out what it is. I am trying to format essentially the same group of cells. If column CI contains the text "TIES MATERIAL" then it should format the cells, in columns CU:DD in the case below, to the color white for that specific row. If that column does not contain the text string and the value has changed from its original value the cell should be changed to the color red.
Here is the code I have for making it white:
Private Sub white_format()
'This section adds the formatting condition of white cells to the cells that are changed by the PEMCON
ActiveWorkbook.Sheets("Material").Activate
Dim lRow As Integer
Dim lCol As Integer
lRow = ActiveWorkbook.Sheets("Material").Range("A2").End(xlDown).Row
lCol = ActiveWorkbook.Sheets("Material").Range("A2").End(xlToRight).Column
firstCell = ActiveWorkbook.Sheets("Material").Range("CU3").Address(False, False)
lastCell = ActiveWorkbook.Sheets("Material").Cells(lRow, lCol).Address(False, False)
Debug.Print "firstCell: " & firstCell
Debug.Print "lastHeaderCell: " & lastHeaderCell
Debug.Print "colCount: " & colCount
'Defines the array of the CU3 to the last used cell and it checks to see if the coorisponding cell in column CI has TIES MATERIAL in it
ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions.Add Type:=xlExpression, Formula1:="=$CI3=""TIES MATERIAL"""
ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).SetFirstPriority
With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 16777215 'this is the color white
.TintAndShade = 0
End With
ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).StopIfTrue = True
End Sub
Here is the code I have for making it red:
Private Sub Red_Format()
Dim lRow As Integer
Dim lCol As Integer
lRow = ActiveWorkbook.Sheets("Material").Range("A2").End(xlDown).Row
lCol = ActiveWorkbook.Sheets("Material").Range("A2").End(xlToRight).Column
firstCell = ActiveWorkbook.Sheets("Material").Range("CU2").Address(False, False)
lastCell = ActiveWorkbook.Sheets("Material").Cells(lRow, lCol).Address(False, False)
formatRange = ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell)
lastHeaderCell = ActiveWorkbook.Sheets("Material").Cells(2, lCol).Address(False, False)
colCount = ActiveWorkbook.Sheets("Material").Range(firstCell, lastHeaderCell).Columns.Count
'Defines the array of the CU2 to the last used cell and adds the formatting to turn it red if it has been altered.
ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions.Add Type:=xlExpression, Formula1:="=OFFSET($A$1,ROW()-1,COLUMN()-1)<>OFFSET($A$1,ROW()-1,COLUMN()+" & colCount & ")"
'ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).SetFirstPriority
With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).StopIfTrue = False
End Sub
Here is what the conditional format looks like when I call the white_format
then call the red_format
in the same sub-routine.
The formulas show up correctly but the colors are in the opposite sections that they need to be in. What am I doing wrong? I also know that my code is not the most efficient that it could/should be. How else could I re-write it?