Hi guys,
Here's hoping somebody can assist in helping me modify an existing code shown below.
The current code adds some conditional formatting to an activesheet and is working great.
I need to add another bit of conditional formatting which is =AND(E22=B22,G22<D22/1.2)
The code needs to loop through the sheet and be added to every third column until the last column as the existing doc does
This is existing coed that is working great.
Sub Add_Conditional_Formatting_2Letters()
Dim LastCol As Long
Dim NextCol As Long
Dim rg As Range
Dim s1 As String
Dim s2 As String
With ActiveSheet
LastCol = .Cells(22, Columns.Count).End(xlToLeft).Column
For NextCol = 7 To LastCol Step 3
Set rg = .Range(Cells(22, NextCol), Cells(80, NextCol))
s1 = Replace(rg.Offset(, -2).Address, "$", "")
s2 = Replace(rg.Offset(, -5).Address, "$", "")
With rg _
.FormatConditions.Add(Type:=xlExpression, _
Formula1:="=" & s1 & "<>" & s2)
.Interior.Color = RGB(0, 204, 205) 'Blue
.Font.Color = RGB(0, 0, 0) 'Black
End With
Next
End With
End Sub
This is code I have tried to modify without any success
Sub Add_Conditional_Formatting_2Lettersmodded()
Dim LastCol As Long
Dim NextCol As Long
Dim rg As Range
Dim s1 As String
Dim s2 As String
Dim s3 As String 'new line added
Dim s4 As String 'new line added
With ActiveSheet
LastCol = .Cells(22, Columns.Count).End(xlToLeft).Column
For NextCol = 7 To LastCol Step 3
Set rg = .Range(Cells(22, NextCol), Cells(80, NextCol))
s1 = Replace(rg.Offset(, -2).Address, "$", "")
s2 = Replace(rg.Offset(, -5).Address, "$", "")
s3 = Replace(rg.Offset(, 0).Address, "$", "") 'new line added
s4 = Replace(rg.Offset(, -3).Address, "$", "") 'new line added
With rg _
.FormatConditions.Add(Type:=xlExpression, _
Formula1:="=" & s1 & "<>" & s2)
.Interior.Color = RGB(0, 204, 205) 'Blue
.Font.Color = RGB(0, 0, 0) 'Black
End With
With rg _
.FormatConditions.Add(Type:=xlExpression, _
Formula1:="=" & "AND" & "(" & s1 & "=" & s2 & "," & s3 & "<" & s4 & "/" & "1.2") '=AND(E22=B22,G22<D22/1.2
.Interior.Color = RGB(255, 0, 0) 'Blue
.Font.Color = RGB(255, 255, 255) 'Black
End With
Next
End With
End Sub
Many thanks for any help or advce
Post also added here https://www.mrexcel.com/forum/excel-...ml#post4806937
Bookmarks