Conditional formatting Macro not working as intended.

Kartik0698

New Member
Joined
May 20, 2019
Messages
2
Hi there,
This is the macro code that I am using to apply conditional formatting:
Code:
Sub Iden_Match()
'
' Iden_Match Macro
' Identify matching cells
'
' Keyboard Shortcut: Ctrl+Shift+K
'
    Range("H3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlUp)).Select
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=J3=F3"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Bold = True
        .Italic = False
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
Basically what I am trying to achieve is that if values in column J equals values in column F, then formatting changes in column H. But it is not working as expected(i.e. it also changes the formatting of cells for which the formula returns false. Image for reference:
https://ibb.co/0sKX2LG
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi & welcome to MrExcel
How about
Code:
Sub Kartik0698()
   With Range("H3", Range("H" & Rows.Count).End(xlUp))
      .FormatConditions.Add xlExpression, , "=J3=F3"
      With .FormatConditions(.FormatConditions.Count)
         .Font.Bold = True
         .Font.Italic = False
         .Interior.Color = 5296274
      End With
   End With
End Sub
 
Upvote 0
This code works absolutely fine. Can you please tell me the problems with my macro recording and how did you overcome those? Thanks in advance.
 
Upvote 0
I have no idea why your code was not working, but you very rarely need to select anything with VBA. So I just re-wrote your code to get rid of the selections.
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,026
Members
449,061
Latest member
TheRealJoaquin

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