Results 1 to 3 of 3

Add conditional formatting via VBA and loop through activesheet

Threaded View

  1. #1
    Registered User
    Join Date
    03-20-2017
    Location
    Lancashire, England
    MS-Off Ver
    2010
    Posts
    40

    Add conditional formatting via VBA and loop through activesheet

    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
    Last edited by Andy15; 04-20-2017 at 11:58 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Add conditional formatting and loop through activesheet
    By Andy15 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-17-2017, 05:31 PM
  2. To loop up Conditional Formatting
    By darksquare in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-09-2016, 04:23 AM
  3. [SOLVED] For loop - Conditional Formatting
    By gombi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-24-2014, 12:32 PM
  4. Loop within Conditional Formatting
    By daaronfriedman in forum Excel General
    Replies: 3
    Last Post: 01-12-2012, 10:18 AM
  5. VB Macro, FOR loop with conditional formatting
    By RMGreenlight in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-02-2010, 03:14 PM
  6. Conditional formatting in a loop
    By surya9 in forum Excel General
    Replies: 1
    Last Post: 02-19-2010, 04:09 PM
  7. activesheet in loop
    By francois05 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-18-2005, 08:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1