简体   繁体   中英

Match text from column within a certain cell - Excel

I have a column of few thousand filenames that are not uniform. For instance:

|     Column A     | Column B |
===============================
| junk_City1_abunc | City1    |
-------------------------------
| nunk_City1_blahb | City1    |
-------------------------------
| small=City2_jdjf | City2    |
-------------------------------
| mozrmcity3_somet | City3    |

I would like to identify the city within the text in column A and return it in Column B.

I've come up with a complex formula that does the trick, but it is difficult to adjust if more cities are added within the filenames in new entries within column A.

Here is an example:

=IF(ISNA(MATCH("*"&$W$3&"*",I248,0)),IF(ISNA(MATCH("*"&$W$4&"*",I248,0)),IF(ISNA(MATCH("*"&$W$5&"*",I248,0)),IF(ISNA(MATCH("*"&$W$6&"*",I248,0)),IF(ISNA(MATCH("*"&$W$7&"*",I248,0)),IF(ISNA(MATCH("*"&$W$8&"*",I248,0)),"Austin","Orlando"),"Las Vegas"),"Chicago"),"Boston"),"Las Angeles"),"National")

It seems like there should be an easier way to do it, but I just can't figure it out.

(To make matters worse, not only am I identifying a city within the filename, I'm looking for other attributes to populate other columns)

Can anyone help?

Here's a round about way that works, not all my own work but a mish mash of bits from other sources:

Assuming the sheet is setup as follows:

起始位置

The formula to use is below, this must be entered using Ctrl + Shift + Enter

=INDEX($C$2:$C$8,MAX(IF(ISERROR(SEARCH($C$2:$C$8,A2)),-1,1)*(ROW($C$2:$C$8)-ROW($C$2)+1)))

Annotated version:

=INDEX([List of search terms],MAX(IF(ISERROR(SEARCH([List of search terms],[Cell to search])),-1,1)*(ROW([List of search terms])-ROW([Cell containing first search term])+1)))

Use the formula =IFERROR(LOOKUP(1E+100,SEARCH($E$2:$E$11,A2),$E$2:$E$11),A2)

This does *****NOT***** have to be array entered.

Where $E$2:$E$11 is the list of names you want returned and A2 is the cell to test

If no matches are found instead of errors you will just use the full name in column b.

If you want errors or expect to NEVER have then you can just use:

=LOOKUP(1E+100,SEARCH($E$2:$E$11,A2),$E$2:$E$11)

在此处输入图片说明

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