How do I use VBA to delete only certain conditional formatting rules applying to a range?

whereswaller

New Member
Joined
Aug 27, 2013
Messages
12
I am using VBA code to add a conditional formatting rule to a dynamically selected column of cells in a worksheet (specified by SelectedCol variable). This is working satisfactorily (see below Code Extract).

I, however, would also like to use VBA code to remove this conditional formatting from a dynamically selected column of cells in a worksheet (using SelectedCol variable) when desired. I do not know how to select the appropriate conditional formatting rule for deletion once I've located the corresponding cell range. Can anyone help and/or share code that does this?

Note, I cannot simply remove all conditional formatting rules as there are other conditional formatting rules that also apply to the same cell range.

Code Extract:
Code:
[COLOR=#222222][FONT=Verdana]Sub AddRemovePublicHoliday()
[/FONT][/COLOR][/FONT][/COLOR]Dim FindString As String
    Dim Rng As Range
    Dim ColNum As Long
    Dim SelectedCol As String
    FindString = Range("B2")


    If (Trim(Range("B1")) <> "" And Trim(FindString) <> "") Then


        If Trim(Range("B1")) = "Add Public Holiday" Then


            With Sheets("Leave Register").Range("D2:ND2") 'searches range
             Set Rng = .Find(What:=DateValue(FindString), _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlFormulas, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False, _
                    SearchFormat:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True  'value found
                ColNum = ActiveCell.Column
                Dim vArr
                vArr = Split(Cells(1, ColNum).Address(True, False), "$")
                SelectedCol = vArr(0)


                MsgBox SelectedCol


                 Columns(SelectedCol & ":" & SelectedCol).FormatConditions.Add Type:=xlExpression, Formula1:= _
                 "=ROW()>2"
                    Columns(SelectedCol & ":" & SelectedCol).FormatConditions(Columns(SelectedCol & ":" & SelectedCol).FormatConditions.Count).SetFirstPriority
                    With Columns(SelectedCol & ":" & SelectedCol).FormatConditions(1).Interior
                        .PatternColorIndex = xlAutomatic
                        .Color = vbBlack
                        .TintAndShade = 0
                    End With
                    With Columns(SelectedCol & ":" & SelectedCol).FormatConditions(1).Font
                        .Color = vbWhite
                        .TintAndShade = 0
                    End With


                Range("A1:A2").Select
                Sheets("Add-Remove Public Holiday").Select
                Range("B1").Select
                Application.CutCopyMode = False
                Selection.ClearContents
                Range("B2").Select
                Selection.ClearContents
                Range("A1").Select


                MsgBox "Public holiday on " & FindString & " added."


            Else


                MsgBox "Form incomplete. Cannot add or remove public holiday1."


            End If


        End With


    ElseIf Trim(Range("B1")) = "Remove Public Holiday" Then


    Sheets("Leave Register").Select




    ' code to delete specific conditional formatting


        MsgBox "Public holiday on " & FindString & " removed."


    Else


        MsgBox "Form incomplete. Cannot add or remove public holiday2."


    End If


    Else


    MsgBox "Form incomplete. Cannot add or remove public holiday3."


    End If
[COLOR=#242729][FONT=Arial][COLOR=#222222][FONT=Verdana]    End Sub[/FONT][/COLOR][/FONT][/COLOR]

 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
The general approach is something like this.
Code:
                    'Adding a format condition
		    Dim FC As FormatCondition
                    Set FC = Columns(SelectedCol & ":" & SelectedCol).FormatConditions.Add(Type:=xlExpression, Formula1:="=ROW()>2")

                    With FC
                        .SetFirstPriority
                        With .Interior
                            .PatternColorIndex = xlAutomatic
                            .Color = vbBlack
                            .TintAndShade = 0
                        End With

                        With .Font
                            .Color = vbWhite
                            .TintAndShade = 0
                        End With
                    End With

Code:
            ' code to delete specific conditional formatting
            With Columns(SelectedCol & ":" & SelectedCol)
                For Each FC In .FormatConditions
                    If FC.Type = xlExpression And (FC.Formula1 = "=ROW()>2") Then
                        FC.Delete
                    End If
                Next FC
            End With
 
Upvote 0
on behalf of the OP, thank you for the solution. It was very helpful.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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