Search code examples
regexgoogle-sheetsgoogle-sheets-formula

Conditional Formatting if Cells in Column Contain Exact Match, Partial or None of Values in Cell


I'm stumped on trying to apply conditional formatting to a column based on a cell at the top of the column when the highlighting is dependent on exact match or inverse of match, partial match, or no match. See example Google Sheet here.

Here's the criteria...

1. Exact matches can be exact or also the inverse of what's in A1. For an exact match, the cells in the column that match A1 would be colored green. But, an exact match could be X,Y, or the inverse Y,X. The point is that an exact match has to have those 2 values, the order doesn't matter.

2. For partial matches, they need to be colored orange. A partial match should have one of the values from A1 in the cell. For example, if A1 has B,D, then any value can precede or follow a match of one of the values. (B,) or (,B) or (D,) or (,D) or just B by itself or just D by itself.

3. Any cells with none of the values from A1 in them are highlighted red. So, any cell that does not contain any of the values from A1 should be red.

See spreadsheet link above for further explanation and examples.


Solution

  • Here you have an option with conditional formatting. I don't know if I fully understood your requirements about orange and red, but here you have an option you may be able to adapt

    If all the options are a match, it will return green. It's done by splitting and comparing each cell with the value in A1. By counting the Filtered data with REGEXMATCH, and comparing to the amount of items in both cells you'll be able to know if every option is a match

    To orange you'll have something similar but without the last step

    And for red you use NOT in combination with REGEXMATCH

    Green: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(A3,",",1,1),(COUNTA(IFNA(FILTER(a,REGEXMATCH(a,JOIN("|",v)))))=COUNTA(v))*(COUNTA(v)=COUNTA(a)))
    
    Orange: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(A3,",",1,1),COUNTA(IFNA(FILTER(a,REGEXMATCH(a,JOIN("|",v))))))
    
    Red: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(TO_TEXT(A3),",",1,1),COUNTA(IFNA(FILTER(a,NOT(REGEXMATCH(a,JOIN("|",v))))))=COUNTA(a))
    

    enter image description here