Quickly Clear Conditional Formatting


April 2, 2008 – 2:29 am by JP

Here is a simple sub that deletes conditional formatting from a selection. The code does some prelim checking to see that it is actually acting on a range, and it mimics Excel’s usual behavior (like a good VBA procedure should, if I may say so) by acting on the entire worksheet if you only select a single cell. However I recommend just selecting the range before you run the code.


Sub Clear_Cond_Formatting()
Dim rng As Excel.Range

If TypeName(Selection) <> "Range" Then Exit Sub

Select Case Selection.Cells.count
    Case 1
       Set rng = Cells
    Case Else
       Set rng = Selection
End Select

On Error Resume Next
rng.FormatConditions.Delete
On Error GoTo 0
End Sub

Enjoy,
JP


If you enjoyed this page, share it!
    StumbleUpon Technorati Digg Google del.icio.us MisterWong TwitThis

Print This Post Print This Post  |  Email This Post Email This Post  |  rss Subscribe to Posts Feed  |  rss Subscribe to Comments

Filed Under: Excel, VBA
Tags: , ,

  1. 3 Responses to “Quickly Clear Conditional Formatting”:

  2. Aren’t the second and third “If Typename” lines redundant? If it’s not a range, then it’s already left the sub.

    By Jon Peltier on Apr 3, 2008

  3. Thanks Jon. That’s what I get for cutting and pasting without reading!

    –JP

    By JP on Apr 3, 2008

  4. Here is the revised code…

    Sub Clear_Cond_Formatting()
    Dim rng As Excel.Range

    If TypeName(Selection) <> “Range” Then Exit Sub

    Select Case Selection.Cells.count
    Case 1
    Set rng = Cells
    Case Else
    Set rng = Selection
    End Select

    On Error Resume Next
    rng.FormatConditions.Delete
    On Error GoTo 0

    End Sub

    By JP on Apr 3, 2008

Post a Comment


Certain comments are subject to moderation and may not appear immediately. First-time comments are moderated. You can use HTML tags in your comment. If you include a greater-than or less-than sign or anything else that could be interpreted as HTML, you need to escape those characters. To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Simple Word macro/button to print out a form || Training class update »