Consulting

Results 1 to 3 of 3

Thread: vba conditional formatting - compare range of adjacent cells

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    57
    Location

    vba conditional formatting - compare range of adjacent cells

    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
    Last edited by Aussiebear; 08-08-2018 at 03:13 PM. Reason: Tidied up the code presentation

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    57
    Location
    P45cal,

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

Posting Permissions

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