查找单元格是否包含查找列中的文本,返回第三个。

huangapple go评论57阅读模式
英文:

Lookup if cell contains text from lookup columns return third

问题

I am trying to lookup if a cell contains strings from two columns in a lookup table and return a category (third column of lookup table).

Cell examples:

abc 2021 Gross Profit abcd
ab 2022 Gross Profit ADJ abcde
ab ADJ 2021 Gross Profit abcde
cd 2023 Payroll asdf
dage Sales 2021 bce
2020 Payroll Revision abcdef

Output should be, respectively:

Cells Column Output Column
abc 2021 Gross Profit abcd Actual
ab 2022 Gross Profit ADJ abcde Adjustment
ab ADJ 2021 Gross Profit abcde Adjustment
cd 2023 Payroll asdf Payroll
dage Sales 2021 bce Sales
2020 Payroll Revision abcdef Payroll Revision

Lookup table example [name: lookup_table]:

Lookup1 Lookup2 Category1
Gross Profit Actual
Gross Profit ADJ Adjustment
Sales Sales
COGS Cost of Goods Sold
Payroll Payroll
Payroll Revision Payroll Adjustment

I am trying to use the following formula to return the category but it is returning a two-value array and does not manage to retrieve the correct category.

英文:

I am trying to lookup if a cell contains strings from two columns in a lookup table and return a category (third column of lookup table).

Cell examples:<br/>

abc 2021 Gross Profit abcd
ab 2022 Gross Profit ADJ abcde
ab ADJ 2021 Gross Profit abcde
cd 2023 Payroll asdf
dage Sales 2021 bce
2020 Payroll Revision abcdef

Output should be; respectivley: <br/>

Cells Column Output Column
abc 2021 Gross Profit abcd Actual
ab 2022 Gross Profit ADJ abcde Adjustment
ab ADJ 2021 Gross Profit abcde Adjustment
cd 2023 Payroll asdf Payroll
dage Sales 2021 bce Sales
2020 Payroll Revision abcdef Payroll Revision

<br/>
Lookup table example [name: lookup_table]:</br>

Edit: Actual lookup table has hundreds of rows this is an example subset - the lookup table should remain an excel table so the reference range adjusts automatically when a new record is added to the lookup_table

Lookup1 Lookup2 Category1
Gross Profit Actual
Gross Profit ADJ Adjustment
Sales Sales
COGS Cost of Goods Sold
Payroll Payroll
Payroll Revision Payroll Adjustment

I am trying to use the following formula to return the category but it is returning a two value array and does not manage to retrieve the correct category:

<br/>

=IFERROR(FILTER(IFERROR(
    LET(cell,M3,
        lookup1_range,lookup_table[Lookup1],
        lookup2_range,lookup_table[Lookup2],
        category_range,lookup_table[Category1],
        contains_both,IF(AND(ISNUMBER(FIND(lookup1_range,cell)), ISNUMBER(FIND(lookup2_range,cell))), 1, 0),
        contains_lookup1,IF(ISNUMBER(FIND(lookup1_range,cell)),1,0),
        matches_lookup1,IF(contains_lookup1,MATCH(lookup1_range,lookup1_range,0),&quot;&quot;),
        matches_both,IF(contains_both,IF(ISNUMBER(FIND(lookup2_range,INDEX(lookup1_range,matches_lookup1))),matches_lookup1,&quot;&quot;),&quot;&quot;),
        matches,IF(matches_both&lt;&gt;&quot;&quot;,matches_both,matches_lookup1),
        lookup_value,IF(matches_both&lt;&gt;&quot;&quot;, INDEX(category_range, matches_both, 1), IF(matches_lookup1&lt;&gt;&quot;&quot;, INDEX(category_range, matches_lookup1, 1), &quot;&quot;)),
        IF(lookup_value&lt;&gt;&quot;&quot;,
            lookup_value,
            IF(matches_lookup1&lt;&gt;&quot;&quot;,
                IF(ISNUMBER(FIND(lookup2_range,cell)),
                    INDEX(category_range, matches_lookup1, 1),
                    INDEX(category_range, matches_lookup1, 1)),
                &quot;&quot;)
        )
    ),
    &quot;&quot;
),IFERROR(
    LET(cell,M3,
        lookup1_range,lookup_table[Lookup1],
        lookup2_range,lookup_table[Lookup2],
        category_range,lookup_table[Category1],
        contains_both,IF(AND(ISNUMBER(FIND(lookup1_range,cell)), ISNUMBER(FIND(lookup2_range,cell))), 1, 0),
        contains_lookup1,IF(ISNUMBER(FIND(lookup1_range,cell)),1,0),
        matches_lookup1,IF(contains_lookup1,MATCH(lookup1_range,lookup1_range,0),&quot;&quot;),
        matches_both,IF(contains_both,IF(ISNUMBER(FIND(lookup2_range,INDEX(lookup1_range,matches_lookup1))),matches_lookup1,&quot;&quot;),&quot;&quot;),
        matches,IF(matches_both&lt;&gt;&quot;&quot;,matches_both,matches_lookup1),
        lookup_value,IF(matches_both&lt;&gt;&quot;&quot;, INDEX(category_range, matches_both, 1), IF(matches_lookup1&lt;&gt;&quot;&quot;, INDEX(category_range, matches_lookup1, 1), &quot;&quot;)),
        IF(lookup_value&lt;&gt;&quot;&quot;,
            lookup_value,
            IF(matches_lookup1&lt;&gt;&quot;&quot;,
                IF(ISNUMBER(FIND(lookup2_range,cell)),
                    INDEX(category_range, matches_lookup1, 1),
                    INDEX(category_range, matches_lookup1, 1)),
                &quot;&quot;)
        )
    ),
    &quot;&quot;
)&lt;&gt;&quot;&quot;),&quot;&quot;)

答案1

得分: 1

You may use COUNTIF combined with wildcards to count text matches:

=IF(COUNTIF(A1, "*Gross Profit*")=0, "None", IF(COUNTIF(A1, "*ADJ*")=0, "Actual", "Adjustment"))

UPDATE: OP wants to relate the lookup table and data, so here's another approach using array formulas:

Because the formula is an array, you must introduce it by pressing CTRL+ENTER+SHIFT or it won't work!

=IFERROR(INDEX(LOOKUPTABLE[Category1], SUMPRODUCT(MAX(COUNTIF(A1, "*" & LOOKUPTABLE[Lookup1] & "*") * COUNTIF(A1, "*" & LOOKUPTABLE[Lookup2] & "*") * ROW(LOOKUPTABLE[Category1])))-1), "None")

Anyways, I've uploaded a sample to Gdrive in case you want to check:

Google Sheets Sample

Notice there is a -1 because the formula returns the absolute row number. Because data on the lookup table starts at row 2, then you must add -1. If data started at row 5, instead of -1, you would need -4 and so on...

英文:

You may use COUNTIF combined with wildcards to count text matches:

查找单元格是否包含查找列中的文本,返回第三个。

=IF(COUNTIF(A1;&quot;*Gross Profit*&quot;)=0;&quot;None&quot;;IF(COUNTIF(A1;&quot;*ADJ*&quot;)=0;&quot;Actual&quot;;&quot;Adjustment&quot;))

UPDATE: OP wants to relationate lookup table and data, so here's another approach using array formulas:

Because the formula is array, you must introduce it pressing <kbd>CTRL</kbd>+<kbd>ENTER</kbd>+<kbd>SHIFT</kbd> or it won't work!

查找单元格是否包含查找列中的文本,返回第三个。

=IFERROR(INDEX(LOOKUPTABLE[Category1],SUMPRODUCT(MAX(COUNTIF(A1,&quot;*&quot; &amp; LOOKUPTABLE[Lookup1] &amp; &quot;*&quot;)* COUNTIF(A1,&quot;*&quot; &amp;LOOKUPTABLE[Lookup2]&amp;&quot;*&quot;)*ROW(LOOKUPTABLE[Category1])))-1),&quot;None&quot;)

Anyways, I've uploaded a sample to Gdrive in case you want to check:

https://docs.google.com/spreadsheets/d/1eJjNzKBw6nLsOkcMhfAK5NyObSgYrUc8/edit?usp=share_link&amp;ouid=114417674018837700466&amp;rtpof=true&amp;sd=true

Notice there is a -1 because the formulla returns the absolute row number. Because data on lookuptable starts at row 2, then you must add -1. If data started at row 5, instead of -1 you would need -4 and so on...

huangapple
  • 本文由 发表于 2023年4月13日 18:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定