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:
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
sFormula = Replace(.Formula1, sFind, sReplace, 1, compare:=vbTextCompare)
If .Formula1 <> sFormula Then
Debug.Print ws.Name & “: ” & .AppliesTo.Address
lCount = lCount + 1
If lCount > 0 Then
MsgBox “All done, a few issues were found”, vbCritical
MsgBox “No issues!”, vbInformation
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.