简体   繁体   中英

Excel String lookup within String

在此处输入图像描述

i have list of "Place of receipt", i need to lookup with another column with set of list that contains City names in Proper case/ Proper name.

i tried below formula but i did not found any luck

=(VLOOKUP(LOOKUP(2^15,SEARCH($I$1:$I$7,A2),$I$1:$I$7),$I$1:$I$7,1,0))

it gives me result when the first word from cell matches with another one, but it will throw error when "place of receipt" has different word after first word,

for example "BRADFORD GB" will not get "Bradford, WYK" from city list as shown in above image.

You can use this formula to capture the first word of the cell (or the entire word if it only contains one word):

=IFERROR(LEFT(A2,FIND(" ",A2)-1),A2)

You can use that to perform a VLOOKUP, if your current formula fails:

=IFERROR([your-current-formula],VLOOKUP(IFERROR(LEFT(A2,FIND(" ",A2)-1),A2),1,1)

i came across a solution to find string before comma and space, if the first word matches it will check the word (+3) which is after the comma. the result is now expected.

The formula i used:

=IFERROR(VLOOKUP(LOOKUP(2^15,SEARCH((LEFT(A2,(FIND(",",A2,1)+3))),$I$1:$I$8,1),$I$1:$I$8),$I$1:$I$8,1,0),(VLOOKUP(LOOKUP(2^15,SEARCH((LEFT(A2,(FIND(" ",A2,1)-1))),$I$1:$I$8,1),$I$1:$I$8),$I$1:$I$8,1,0))) 

Solution

在此处输入图像描述

Assuming you have office excel 365, you can use combination of xlookup and Find function to solve as below

=XLOOKUP("*"&IFERROR(LEFT(A2,FIND("@",SUBSTITUTE(A2,",","@",2))-1),LEFT(A2,FIND(" ",A2)-1))&"*",$D$2:$D$7,$D$2:$D$7,"Not found",2)

The result was obtained as below

结果

Explanation

  1. First we need to extract the characters upto the second iteration of ","

    LEFT(A2,FIND("@",SUBSTITUTE(A2,",","@",2))-1)

  2. Since not all strings have Names with "," example: BRADFORD GB, the above formula will throw error to counter that we use IFERROR function. So, if the above formula throws error we are going to extract only characters upto first instance of space.

    IFERROR(LEFT(A2,FIND("@",SUBSTITUTE(A2,",","@",2))-1), LEFT(A2,FIND(" ",A2)-1) )

  3. Now we just need to Lookup the name on the list of cities. However since Place of Receipt and List of cities are not exactly the same, we are going to use wildcard "*" as in the overall formula on the top.

=XLOOKUP("*"&IFERROR(LEFT(A2,FIND("@",SUBSTITUTE(A2,",","@",2))-1),LEFT(A2,FIND(" ",A2)-1))&"*",$D$2:$D$7,$D$2:$D$7,"Not found",2)

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