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.

VBA:
  1. Sub Clear_Cond_Formatting()
  2. Dim rng As Excel.Range
  3.  
  4. If TypeName(Selection) <> "Range" Then Exit Sub
  5.  
  6. Select Case Selection.Cells.count
  7.     Case 1
  8.        Set rng = Cells
  9.     Case Else
  10.        Set rng = Selection
  11. End Select
  12.  
  13. On Error Resume Next
  14. rng.FormatConditions.Delete
  15. On Error GoTo 0
  16. End Sub

Enjoy,
JP



Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Excel, VBA
Tags: , ,

This post has 134 views since April 2, 2008 – 2:29 am.
  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

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].


Browse Posts:


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