PDA

View Full Version : [SOLVED:] vba conditional formatting - compare range of adjacent cells



SteveM99
08-03-2018, 07:24 AM
I need help with coding vba conditional formatting. I am trying to compare a column of numbers to each other and color the cells which do not match. Either color both cells or the cells in the right column (column J).

I researched and attempted but got stuck and needed some help. Below is my code


Sub AA450_CONDITIONAL1()
Dim rg As Range
Dim cond1 As FormatCondition
'GET LAST ROW IN COLUMN J IN THE RECAP SHEET FOR THIS MACRO
Dim Lastrow7 As Long
Lastrow7 = Worksheets("RECAP").Columns("O").Find("*", SEARCHDIRECTION:=xlPrevious, SEARCHORDER:=xlByRows, LookIn:=xlValues).Row
'SET THE RANGE
Set rg = Range("J5:J" & Lastrow7)
'clear any existing conditional formatting
'rg.FormatConditions.Delete
Worksheets("RECAP").Columns("J").FormatConditions.Delete
Worksheets("RECAP").Activate
'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$H$5:$H$" & Lastrow7)
'define the format applied for each conditional format
With cond1
.Interior.Color = vbGreen
.Font.Color = vbWhite
End With
End Sub

p45cal
08-04-2018, 02:07 AM
try:
Sub AA450_CONDITIONAL1()
Dim rg As Range
Dim cond1 As FormatCondition
Dim Lastrow7 As Long

'GET LAST ROW IN COLUMN J IN THE RECAP SHEET FOR THIS MACRO
Lastrow7 = Worksheets("RECAP").Columns("O").Find("*", SEARCHDIRECTION:=xlPrevious, SEARCHORDER:=xlByRows, LookIn:=xlValues).Row
'SET THE RANGE
Set rg = Worksheets("RECAP").Range("J5:J" & Lastrow7)
'clear any existing conditional formatting
rg.EntireColumn.FormatConditions.Delete
'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$H5")
'define the format applied for each conditional format
With cond1
.Interior.Color = vbGreen
.Font.Color = vbWhite
End With
End Sub

SteveM99
08-08-2018, 02:30 PM
P45cal,

Thank you for code mods. You gave me what I needed to make this work effectively. Thank you!!!