Conditional formatting using VBA

wrecclesham

Board Regular
Joined
Jul 24, 2019
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Could someone please help me to create a VBA script which applies the following three conditional formatting rules?

S5Gwoqj.png


Rule 1

q8L8f2A.png


Rule 2

DDn5BMJ.png


Rule 3

GdD9ACk.png
 
Last edited:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Did you try using the macro recorder ??
I managed this, but I'm not sure about the formulas

Code:
Sub Macro1()
With Range("D2:D11")
    .FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK($D$2:$D$11)"
    .FormatConditions(Range("D2:D11").FormatConditions.Count).SetFirstPriority
    .FormatConditions.Add Type:=xlExpression, Formula1:="=OR(IFERROR(TODAY()-D2,0)>=120,ISTEXT(D2))"
    .FormatConditions(Range("D2:D11").FormatConditions.Count).SetFirstPriority
        With Range("D2:D11").FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 5296274
        End With
    .FormatConditions.Add Type:=xlExpression, Formula1:="=TODAY()-D2:D11<120"
    .FormatConditions(Range("D2:D11").FormatConditions.Count).SetFirstPriority
        With Range("D2:D11").FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 10066431
        End With
End With
End Sub
 
Upvote 0
Thanks a lot for that code!

I'm trying to avoid using the macro recorder in this particular case as I want to have clean code that I can use to learn from in the future.
 
Upvote 0
OK, so now use the recorder and compare the 2 codes if you wish to learn.....and I can bet someone will come up with even better code !!
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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