+ Reply to Thread
Results 1 to 6 of 6

Conditional Formatting VBA (Compare 2 column)

Hybrid View

  1. #1
    Forum Contributor NewYears1978's Avatar
    Join Date
    03-15-2010
    Location
    Forney, TX
    MS-Off Ver
    Excel 2016
    Posts
    962

    Conditional Formatting VBA (Compare 2 column)

    I am adding in a column in a long macro and then I need to add conditional formatting to that column in VBA. Struggling.

    I need to compare column V to lastrow to column P to lastrow and have it one color or another.

    This is part of what I had so far but not sure how to define i to mean to use the current row... so I got stuck. Hellllp =D

    Sub JP_AddConditions()
    
        With Range("V" & i).FormatConditions
                .Add Type:=xlExpression, Operator:=xlEquals, Formula1:="=$P$" & i & "<>$V$" & i
            End With
            Range("V" & i).FormatConditions(1).Interior.ColorIndex = 6
      End Sub

  2. #2
    Forum Contributor NewYears1978's Avatar
    Join Date
    03-15-2010
    Location
    Forney, TX
    MS-Off Ver
    Excel 2016
    Posts
    962

    Re: Conditional Formatting VBA (Compare 2 column)

    I forgot to put that I used record to see how it did it and it works but it is really...hefty code. Also I have no clue where the .color values come from so I can't cahnge them easily..what are these long numbers? HEX and RGB won't work.

    Sub TEST3()
    '
    ' TEST3 Macro
    '
    
    '
        Range("V2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=V2=P2"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 423523523
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=V2<>P2"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 323423423
        End With
         Selection.FormatConditions(1).StopIfTrue = False
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=V2=""No Markup"""
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 2344234
            
        End With
        Selection.FormatConditions(1).StopIfTrue = False
    End Sub

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,279

    Re: Conditional Formatting VBA (Compare 2 column)

    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    06-07-2018
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    20

    Re: Conditional Formatting VBA (Compare 2 column)

    So the way this works is:

    Range("V2").Select
        Range(Selection, Selection.End(xlDown)).Select
    As though you were doing it manually, this is saying that the range to be conditionally formatted will span from V2 down to the last row in that column.

    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=V2=P2"
    It then applies the formula
    =V2=P2
    to each cell in the selection, so this addresses your question re: how to define I. In terms of colour, you can use the RGB function here
     .Color = RGB(255,100,100)
    Hope that helps.

  5. #5
    Forum Contributor NewYears1978's Avatar
    Join Date
    03-15-2010
    Location
    Forney, TX
    MS-Off Ver
    Excel 2016
    Posts
    962

    Re: Conditional Formatting VBA (Compare 2 column)

    Dan yes! Broken down that made so much more sense, also made me realize that I always screw up conditionally formatting (because you need that first...starting cell, whatever it is called (the =V2 part).

    I got it working with
    Sub JP_AddConditions()
    
        Range("V2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=V2=P2"
        Selection.FormatConditions(1).Interior.ColorIndex = 7
    
    End Sub
    Thank you kindly, also TMS thanks for replying even though I didn't end up having to use a sample =D

  6. #6
    Registered User
    Join Date
    06-07-2018
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    20

    Re: Conditional Formatting VBA (Compare 2 column)

    You're very welcome, and I'm glad that you understood what I was getting at with the starting cell part. I wasn't quite sure how I would explain it it if you didin't know what I was talking about. And thank you for posting the solution - it will help others in a similar situation, I'm sure.

    What I did do was explain the Long numbers. That's the colour code that Excel understand and it covers the full RGB spectrum. What you've used in your solution above is the ColorIndex property - that has a spectrum of (I think) 56 colours and I believe it's a legacy feature from an older Excel version. But the best solution is the one that works! Thanks for letting us know.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Compare & Conditional Formatting HELP- please
    By SBennett212 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-20-2016, 03:52 PM
  2. [SOLVED] Conditional Formatting - Compare Column with previous Column
    By TicklyTigger in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-21-2016, 06:29 AM
  3. [SOLVED] Compare any value in a column with any value in an other using conditional formatting
    By Soulseeker in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-21-2015, 08:08 AM
  4. Replies: 3
    Last Post: 11-22-2011, 02:06 PM
  5. Replies: 4
    Last Post: 03-31-2011, 01:50 PM
  6. Conditional Formatting Help-What I want to do is compare them
    By manic2511 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-14-2007, 03:55 PM
  7. Compare two Sheets with Conditional Formatting
    By wayliff in forum Excel General
    Replies: 1
    Last Post: 01-13-2006, 08:30 PM

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