Conditional Formatting with VBA

llane23

New Member
Joined
Jan 31, 2023
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
Not working. I am obviously doing something wrong. I just want the G cell to highlight if it's the same as what is in the I cell. And then to go back to no highlight once the conditions are met.

Sub ConditionalFormatFirstNames()
Dim lr As Long
lr = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Row
Dim LastNames As Range
Set LastNames = Range("I:I")
With ActiveSheet.Range("G2:G" & lr)
.FormatConditions.Delete
.FormatConditions.Add xlExpression, Formula1:="=$G2 = ""LastNames.Value"""
.FormatConditions(1).Interior.Color = RGB(156, 0, 6)
End With
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this

VBA Code:
Sub ConditionalFormatFirstNames()
    Dim lr As Long
    lr = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
    Dim LastNames As Range
    Set LastNames = Range("I:I")
    
    With ActiveSheet.Range("G2:G" & lr)
        .FormatConditions.Delete
        .FormatConditions.Add xlExpression, Formula1:="=$G2=$I2"
        .FormatConditions(1).Interior.Color = RGB(156, 0, 6)
    End With
End Sub
 
Upvote 0
Thank you for your help! This is highlighting G1, but I need it to highlight the actual cells that match. Am I kind of on the right track here?
 
Upvote 0
This should work. Test for a match in G/I cols

VBA Code:
Sub ConditionalFormatFirstNames()
    Dim lr As Long
    lr = ActiveSheet.Cells(Rows.Count, "G").End(xlUp).row

    With ActiveSheet.Range("G2:G" & lr)
        .FormatConditions.Delete
        .FormatConditions.Add xlExpression, Formula1:="=$G2=$I2"
        .FormatConditions(1).Interior.Color = RGB(156, 0, 6)
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,094
Latest member
mystic19

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