简体   繁体   中英

Cell shader based on cell formula with conditional formatting

I am trying to develop a code that color-codes cells based on cell contents ie based on the following criteria

  1. Hardcoded cells
  2. Cells with formula
  3. Cells with links to other sheets
  4. Cells with external links

The issue in here is that the code I have been able to develop uses actual background colors to color code the cells.

I am looking for a way to use conditional formatting to run the same code so that I can develop a code to unshade the cells and it restores to original formatting in the sheet.

Can someone help me converting the code to use conditional format?

This answer relies on the FORMULATEXT function which is not available prior to Excel 2013. Also I'm going to crib some code from this useful post which shows a reasonably simple way of adding multiple conditions to cells.

So for your 4 criteria we can use FORMULATEXT for each:

  1. Hardcoded cells: =AND(ISNA(FORMULATEXT(A2)),NOT(ISBLANK(A2)))
  2. Cells with formula: =NOT(ISNA(FORMULATEXT(A2)))
  3. Cells with links to other sheets: =NOT(ISERROR(SEARCH(""*,*"",FORMULATEXT(A2),1)))
  4. Cells with external links: =NOT(ISERROR(SEARCH(""*.xls*]*,*"",FORMULATEXT(A2),1)))

Noting:

  • Formula 1 has the distinction that the logic says any value that is not a formula which most of the time will mean a hard-coded value.

  • Formula 2 is the inverse of Formula 1 in that any non-error with FORMULATEXT means it's a formula

  • Formulas 3 and 4 are per your wildcard logic except I only implemented the Like "*!*" bit and not the other clauses - you can extend the formula for this although it might end up on the complex side.

The order within the conditional format matters. In the code below, the order is actually 1, 4, 3, 2 because 4 is a special case of 3 (ie contains a ! ) and 4 and 3 are special cases of 2 (ie all 3 are formulas).

So the code I used is:

Option Explicit

Sub CellShader()

    Dim rng As Range
    
    Set rng = Sheet1.Range("A2:A6")
    
    ' delete existing conditional formatting
    rng.FormatConditions.Delete
    
    ' add your 4 rules in order 1, 4, 3, 2
    AddRule rng, "=AND(ISNA(FORMULATEXT(A2)),NOT(ISBLANK(A2)))", vbCyan
    AddRule rng, "=NOT(ISERROR(SEARCH(""*.xls*]*!*"",FORMULATEXT(A2),1)))", vbMagenta
    AddRule rng, "=NOT(ISERROR(SEARCH(""*!*"",FORMULATEXT(A2),1)))", vbRed
    AddRule rng, "=NOT(ISNA(FORMULATEXT(A2)))", vbGreen
   
End Sub

' https://stackoverflow.com/questions/40209398/conditional-formatting-vba-multiple-conditions
Sub AddRule(rng As Range, strFormula As String, lngColor As Long)
    With rng.FormatConditions
        With .Add(Type:=xlExpression, Formula1:=strFormula)
            .Interior.Color = lngColor
            .StopIfTrue = True
        End With
    End With
End Sub

Used on these test cases it produces the correct result:

在此处输入图像描述

You can see A2 is blank and is not being formatted as it is a not-a-formula, but does not contain any value (hard-coded or otherwise).

HTH

If you just want to reset the colours you could write a new sub to loop through the cells and set the interior colour back to white or (255, 255, 255)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM