Add conditional formatting via VBA and loop through activesheet

Andy15

Board Regular
Joined
Apr 1, 2017
Messages
56
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)<="" font="">

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 code that is working great.

Code:
[COLOR=#333333]Sub Add_Conditional_Formatting_2Letters()[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">    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 </code>[COLOR=#333333]End Sub[/COLOR]


This is code I have tried to modify without much success

Code:
[COLOR=#333333]Sub Add_Conditional_Formatting_2Lettersmodded()[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">    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 </d22></code>[COLOR=#333333]End Sub[/COLOR]


Many thanks for any help or advice given

Question also posted https://www.excelforum.com/excel-pr...and-loop-through-activesheet.html#post4633636

but unfortunately had no response.</d22>
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How does the new code not work?
 
Upvote 0
Hi,

I get an Run time error '5':
Invalid procedure call or argument and when I enter Debug this section is highlighted

Code:
With rg _
                    .FormatConditions.Add(Type:=xlExpression, _
                     Formula1:="=" & "AND" & "(" & s1 & "=" & s2 & "," & s3 & "<" & s4 & "/" & "1.2")

Thanks
 
Upvote 0
Have you checked that this produces a valid formula?
Code:
"=" & "AND" & "(" & s1 & "=" & s2 & "," & s3 & "<" & s4 & "/" & "1.2")

By the way, to get the address of a range without the $ you can use Address(0,0).
Code:
 s1 = rg.Offset(, -2).Address(0, 0)
 
Upvote 0
Try this edited code
Code:
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
		    .Interior.Color = RGB(0, 204, 205) 'Blue
                    .Font.Color = RGB(0, 0, 0) 'Black
	    End with
End Sub
 
Upvote 0
Add this to the code judge before the line causing the error.
Code:
Debug.Print Formula1:="=" & "AND" & "(" & s1 & "=" & s2 & "," & s3 & "<" & s4 & "/" & "1.2")
That will output the formula to the Immediate Window (CTTL+G).

You can then copy the formula and paste it in a cell to see if it's valid.
 
Upvote 0
Hi Norie,

Sorry if i am not getting what I need to do but when I copy the Debug.Print before the formula as above I get the error "expected named parameter"
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,839
Members
449,193
Latest member
MikeVol

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top