Updating Conditional formatting using VBA adds in conditions rather than replacing them

miless2111s

Active Member
Joined
Feb 10, 2016
Messages
283
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a sheet where users will sometimes paste in elements. In doing so they can replace the conditional formatting in either nothing or some other formats.

I have some VBA on a button designed to replace the formatting with the "correct" conditional formatting (see below). The issue I am facing is that it adds conditions rather than replacing them. This means that after a while, we have lots of conditions, most of them repeats, which can make troubleshooting a bit tricky. I have around 15 of these expressions (I have included a small number for illustration), so I am keen to avoid complexity if possible.

VBA Code:
    'D: Check that item in date range  Rnage: =$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406   Condition: =AND(D3="y",OR(VLOOKUP(A3,MS_Data,3,FALSE)<Rep_start,VLOOKUP(A3,MS_Data,3,FALSE)>Rep_End))
    With Worksheets("Bar Details").Range("$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406").FormatConditions.Add(Type:=xlExpression, Formula1:= _
        "AND(D3=""y"",OR(VLOOKUP(A3,MS_Data,3,FALSE)<Rep_start,VLOOKUP(A3,MS_Data,3,FALSE)>Rep_End))")
            .Font.Bold = False
            .Interior.ColorIndex = 15
            .StopIfTrue = False
    End With
    'd: Check that item has a "Y" when it is in the data  Range: =$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406  Condition: =AND(D3="",NOT(ISERROR(VLOOKUP(A3,MS_Data,1,FALSE))))  Amber
    With Worksheets("Bar Details").Range("$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406").FormatConditions.Add(Type:=xlExpression, Formula1:= _
        "=AND(D3="""",NOT(ISERROR(VLOOKUP(A3,MS_Data,1,FALSE))))")
            .Font.Bold = False
            .Interior.ColorIndex = 45
            .StopIfTrue = False
    End With
    'D: check if the item has a "Y" and it isn't in the data Range:  =$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406  Condition =AND(NOT(D3=""),ISERROR(VLOOKUP(A3,MS_Data,1,FALSE)))  Red
    With Worksheets("Bar Details").Range("$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406").FormatConditions.Add(Type:=xlExpression, Formula1:= _
        "=AND(NOT(D3=""""),ISERROR(VLOOKUP(A3,MS_Data,1,FALSE)))")
            .Font.Bold = False
            .Interior.ColorIndex = 3
            .StopIfTrue = False
    End With

I have one solution, which is to add the same range before each one with .FormatConditions.Delete so something like below however I don't know if this is the best / most efficient way to do it
VBA Code:
    Worksheets("Bar Details").Range("$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406").FormatConditions.Delete
    With Worksheets("Bar Details").Range("$D$3:$D$302,$D$305:$D$330,$D$333:$D$353,$D$356:$D$406").FormatConditions.Add(Type:=xlExpression, Formula1:= _
        "AND(D3=""y"",OR(VLOOKUP(A3,MS_Data,3,FALSE)<Rep_start,VLOOKUP(A3,MS_Data,3,FALSE)>Rep_End))")
            .Font.Bold = False
            .Interior.ColorIndex = 15
            .StopIfTrue = False
    End With

Any help will be appreciated.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You could modify the existing formats (assuming you know that all those cells will definitely already have them) but that delete and reapply approach is the simplest in my opinion.
 
Upvote 0
You could modify the existing formats (assuming you know that all those cells will definitely already have them) but that delete and reapply approach is the simplest in my opinion.
Is there any advantage (speed etc) to doing something like Worksheets("Bars Details").Cells.FormatConditions.Delete?
 
Upvote 0
I very much doubt that there would be any noticeable difference, but you could always try it. ;)
 
Upvote 0
Solution

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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