2
votes

(Due to my organization policies I cannot share a sample sheet.)

I have two sheets with data like so:

Sheet1

enter image description here

Sheet2

enter image description here

Now, in Sheet1, in column C, I am looking for an ARRAYFORMULA that will look up the value in Sheet1!A:A by matching the substring in Sheet2!A:A after removing the spaces from Sheet2!A:A and then returning the value from the row in Sheet2!B:B.

I came up with a non-ARRAYFORMULA formula to get the data per-row (in Sheet1!C:C below). But my real data-sets have thousands of rows and I don't want to have to copy/paste a formula into each one.

enter image description here

Each row has a formula that looks like so:

=TEXTJOIN(
    ", "
  , TRUE
  , IFNA(
        FILTER(
            Sheet2!B:B
          , Sheet2!B:B <> ""
          , Sheet2!A:A <> ""
          , NOT(
                ISERROR(
                    SEARCH(
                        REGEXREPLACE(Sheet2!A:A, " ", "")
                      , A2
                    )
                )
            )
        )
      , "not found"
    )
)

I tried to convert this to an ARRAYFORMULA in Sheet1!B2 but it doesn't yield the expected result. The formula is:

=ArrayFormula(
    TEXTJOIN(
        ", "
      , TRUE
      , IFNA(
            FILTER(
                Sheet2!B:B
              , Sheet2!B:B <> ""
              , Sheet2!A:A <> ""
              , NOT(
                    ISERROR(
                        SEARCH(
                            REGEXREPLACE(Sheet2!A:A, " ", "")
                          , A2:A
                        )
                    )
                )
            )
          , "not found"
        )
    )
)
1
Would you consider using Apps Script for this?Iamblichus
Nope. Can't for various reasons.IMTheNachoMan
@IMTheNachoMan Sorry about the wrong answer. Could you please update your problem statement to mention the substring part?Calculuswhiz
Just did. So sorry -- not sure how I missed that.IMTheNachoMan

1 Answers

3
votes

Ok, I finally got it to work with the substring and SEARCH.

=Array_Constrain(TRANSPOSE(ArrayFormula(REGEXREPLACE(REGEXREPLACE(SPLIT(TEXTJOIN(", ",1,{ArrayFormula(IFERROR(HLOOKUP("Value",Sheet2!B:B,ArrayFormula(Transpose(SEQUENCE(COUNTA(Sheet2!A2:A),1,2))*(SEARCH(TRANSPOSE(SUBSTITUTE(FILTER(Sheet2!A2:A,LEN(Sheet2!A2:A))," ",)),FILTER(A2:A,LEN(A2:A)))>0)),0))),ArrayFormula(IF(SEQUENCE(COUNTA(A2:A)),";",""))}),", ;",0,0),"^, ",),"^$","not found"))),COUNTA(A2:A),1)

"Readable" version:

=Array_Constrain(
    TRANSPOSE(
        ArrayFormula(REGEXREPLACE(
            REGEXREPLACE(
                SPLIT(
                    TEXTJOIN(
                        ", ",
                        1,
                        {
                            ArrayFormula(IFERROR(
                                HLOOKUP(
                                    "Value",
                                    Sheet2!B:B,
                                    ArrayFormula(
                                        Transpose(
                                            SEQUENCE(COUNTA(Sheet2!A2:A),1,2)
                                        )*
                                        (SEARCH(
                                            TRANSPOSE(
                                                SUBSTITUTE(
                                                    FILTER(
                                                        Sheet2!A2:A,
                                                        LEN(Sheet2!A2:A)
                                                    ),
                                                    " ",
                                                )
                                            ),
                                            FILTER(A2:A,LEN(A2:A))
                                        )>0)
                                    ),
                                    0
                                )
                            )),
                            ArrayFormula(IF(
                                SEQUENCE(COUNTA(A2:A)),
                                ";",
                                ""
                            ))
                        }
                    ),
                    ", ;",
                    0,
                    0
                ),
                "^, ",
            ),
            "^$",
            "not found"
        ))
    ),
    COUNTA(A2:A),
    1
)