VBA Adding and removing conditional formatting

TeadyBeeR

New Member
Joined
May 20, 2015
Messages
9
Hello all,

I have an excel file with 3 checkboxes that enable and disable conditional formatting (to find duplicates) using something like this:

Code:
Sub ChkBxA()

    Dim rg As Range
    Set rg = Range("F6:H59,J6:CK47,CW6:CW1000")
    Dim Chk As CheckBox


    Set Chk = ActiveSheet.CheckBoxes(Application.Caller)


    If Chk = 1 Then
        Dim uv As UniqueValues
        Set uv = rg.FormatConditions.AddUniqueValues
        uv.DupeUnique = xlDuplicate
        uv.Interior.Color = RGB(205, 250, 200)
        uv.SetFirstPriority
    Else
        rg.FormatConditions(1).Delete
    End If

End Sub

All three are almost identical and only have slightly different ranges (they are partially overlapping) and use different formatting style. Everything works fine as long as I disable those in an opposite order from the order I was turning them on. For example, if I enable them in this order A -> B -> C and disable them in the C -> B -> A order, everything will be fine - highlights will turn on, turn off and there won't be any remaining conditional formatting rules left.

Unfortunately, If I disable them in a different order (for example turn on: A -> B -> C and turn off: A -> B -> C), they won't be disabled properly and I will end up with a broken conditional formatting still working in the file.

Is there a way to make sure that each macro removes conditional formatting that it enabled and not just the last one?

Thanks,
TB
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
The following occurred to me, just change the RGB color to the color number.

Code:
Sub ChkBxA()
    Dim rg As Range, Chk As CheckBox, uv As UniqueValues, wcolor As Variant
    
[COLOR=#0000ff]    wcolor = 13171405[/COLOR]
    Set rg = Range("F6:H59,J6:CK47,CW6:CW1000")
    Set Chk = ActiveSheet.CheckBoxes(Application.Caller)
    If Chk = 1 Then
        Set uv = rg.FormatConditions.AddUniqueValues
        uv.DupeUnique = xlDuplicate
        uv.Interior.Color = wcolor ' RGB(205, 250, 200)
        uv.SetFirstPriority
    Else
        For i = 1 To 3
            Set uv = rg.FormatConditions.Item(i)
            If uv.Interior.Color = wcolor Then
                rg.FormatConditions(i).Delete
                Exit For
            End If
        Next
    End If
End Sub


Sub ChkBxB()
    Dim rg As Range, Chk As CheckBox, uv As UniqueValues, wcolor As Variant
    
[COLOR=#0000ff]    wcolor = 13133005[/COLOR]
    Set rg = Range("F6:H59,J6:CK47,CW6:CW1000")
    Set Chk = ActiveSheet.CheckBoxes(Application.Caller)
    If Chk = 1 Then
        Set uv = rg.FormatConditions.AddUniqueValues
        uv.DupeUnique = xlDuplicate
        uv.Interior.Color = wcolor
        uv.SetFirstPriority
    Else
        For i = 1 To 3
            Set uv = rg.FormatConditions.Item(i)
            If uv.Interior.Color = wcolor Then
                rg.FormatConditions(i).Delete
                Exit For
            End If
        Next
    End If
End Sub


Sub ChkBxC()
    Dim rg As Range, Chk As CheckBox, uv As UniqueValues, wcolor As Variant
    
[COLOR=#0000ff]    wcolor = 6579405[/COLOR]
    Set rg = Range("F6:H59,J6:CK47,CW6:CW1000")
    Set Chk = ActiveSheet.CheckBoxes(Application.Caller)
    If Chk = 1 Then
        Set uv = rg.FormatConditions.AddUniqueValues
        uv.DupeUnique = xlDuplicate
        uv.Interior.Color = wcolor
        uv.SetFirstPriority
    Else
        For i = 1 To 3
            Set uv = rg.FormatConditions.Item(i)
            If uv.Interior.Color = wcolor Then
                rg.FormatConditions(i).Delete
                Exit For
            End If
        Next
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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