Search code examples
google-sheetsgoogle-sheets-formula

Dynamic Comparison Operators in Sheets


In B3, I have this formula: =B2&REGEXEXTRACT(G2,"=|<>"&C2). And the result I get is: 1=2. However, I need the result to be either TRUE or FALSE.

I tried using =TRUE(B2&REGEXEXTRACT(G2,"=|<>"&C2)), but I receive an error.

Screenshot

I need the result to be either TRUE or FALSE.


Solution

  • Your issue is that your formula generates a string result, and there's no simple way to force the evaluation of a string as a formula in Google Sheets. A hacky approach is to abuse QUERY, because the select string is in text format:

    =not(iserror(query({B2,C2},"select Col1 where Col1"&G2&"Col2",0)))
    

    What is happening here is that the QUERY returns the value in Col1 (B2) if the comparison is TRUE or an #N/A error if it is false; ISERROR will therefore return the 'wrong' answer, and so NOT flips the answer to the correct one.