Find and Replace In Conditional Formats

Yesterday I made a comment on Twitter that I was trying to figure out if it would be faster to check the conditional formatting formulas in 40 workbooks manually to ensure they were correct, vs writing a tool to do it.  My buddy Jon Peltier tweeted back saying “Don't know about faster, but it's got to be more fun to code it.”  I thought about it, but then when I found that one workbook returned a count of 250 rules… the answer was to code it.

Well, yes, Jon was right… if your definition of fun is akin to performing home dentistry for your root canal.

The code I used to modify my conditional formats has been incredibly unreliable, and is returning different effects depending on the users’ machine that it is run on, the version of Excel, and maybe a few other things.  (It did different things after I had lunch too!)

I finally gave up on re-writing the conditional formatting rules, and ended up just running a macro to tell me which formulas I needed to edit:

[vb]Sub FindCFIssues()

    Dim ws As Worksheet
    Dim ftc As FormatCondition
    Dim sFormula As String
    Dim sFind As String
    Dim sReplace As String
    Dim lCount As Long

    On Error Resume Next
    sFind = "ops-Internal"
    sReplace = "ops_Internal"

    For Each ws In ActiveWorkbook.Worksheets
        For Each ftc In ws.UsedRange.FormatConditions
            With ftc
                sFormula = Replace(.Formula1, sFind, sReplace, 1, compare:=vbTextCompare)
                If .Formula1 <> sFormula Then
                    Debug.Print ws.Name & ": " & .AppliesTo.Address
                    lCount = lCount + 1
                End If
            End With
        Next ftc
    Next ws

    If lCount > 0 Then
        MsgBox "All done, a few issues were found", vbCritical
    Else
        MsgBox "No issues!", vbInformation
    End If
End Sub

[/vb]

Ultimately, this code searches for “ops-internal”, and flags my attention, because it should be “ops_Internal”.

Some weirdness here too… the “Next ftc” portion of the code runs until it’s gone through all the rules, then errors.  I had to add the On Error Resume Next above in order to let it continue, as it choked with just “Next” inside the other loop.

So at least I’ve got something now that quickly tells me if I have an issue, and prints out the offending rules in the VBA Immediate window.  I’d rather have the replacement done, but really don’t have the time to work through all the idiosyncrasies… err.. I mean fun… that will be involved in getting it right.

Share:

Facebook
Twitter
LinkedIn

3 thoughts on “Find and Replace In Conditional Formats

  1. Hey David,

    Agreed. My original hope was to also make the find and replace work, but with the instability I experienced, (the formula I placed wasn't what Excel ended up applying) it killed it for me (at least for the short term.)

    Giving a UI to this part shouldn't be too tough though. 🙂

  2. Pingback: The Ken Puls (Excelguru) Blog » Blog Archive » Conditional Formatting Formula Inspector

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Posts