问题
I am trying to get this sort of result, where I check on sheet1 if the first three numbers on column A are found anywhere in the sheet2's column A. If the numbers are found then on column B excel prints Yes and if it is not found excel prints No:
Sheet 1
A B
=12300 Yes
=56700 Yes
=89200 No
Sheet 2
A
=12345
=56789
=44498
The formula that I have at the moment on sheet1 col B is
=IF(ISNUMBER(MATCH(A1;Sheet2!A:A));"Yes";"No")
How to add the possibility to check that it takes into consideration only the 3 first characters in both sheets?
回答1:
Alternatively, you can also use a SUMPRODUCT
:
=IF(SUMPRODUCT(--(LEFT(A1,3)=LEFT(Sheet2!$A$1:$A$3,3))),"Yes","No")
回答2:
You can initially try:
=IF(ISNUMBER(MATCH(LEFT(A1,3),LEFT(Sheet2!A1:A3,3),0)),"YES","NO")
And make sure to enter as array through CtrlShiftEnter
Notice the reference of the used range, with array formulas you would want to try and avoid whole column references because of performance issues. In this specific case we could make it dynamic through:
=IF(ISNUMBER(MATCH(LEFT(A1,3),LEFT(Sheet2!A1:INDEX(Sheet2!A:A,COUNTA(Sheet2!A:A)),3),0)),"YES","NO")
Now we got a dynamic formula, we might as well try to avoid the use of having to enter it as an array formula. We could implement an INDEX
to take care of that issue:
=IF(ISNUMBER(MATCH(LEFT(A1,3),INDEX(LEFT(Sheet2!A1:INDEX(Sheet2!A:A,COUNTA(Sheet2!A:A)),3),),0)),"YES","NO")
So there we have it; a dynamic array formula that avoids having to enter as such =)
If your values actually start with a =
then you should reference the first 4 characters of the value with LEFT
. And to also take into consideration your parameters are seperated through semi-colon, the final formula would look like:
=IF(ISNUMBER(MATCH(LEFT(A1;4);INDEX(LEFT(Sheet2!A1:INDEX(Sheet2!A:A;COUNTA(Sheet2!A:A));4););0));"YES";"NO")
回答3:
There is many perfect answers to this question, but it can also be done by adding a wildcard to the end of your string instead of the actual value:
=IF(ISNUMBER(MATCH(LEFT(A1,3)&"*",Sheet2!A:A,0)),"Yes","No")
As @JvdV pointed out, this doesn't work properly for numeric values, only for text. As per his suggestion:
=IF(ISNUMBER(MATCH(LEFT(A1,3)&"*",INDEX(TEXT(Sheet1!A:A,"0"),),0)),"Yes","No")
Would do to omit this issue. Also please read the comments for his suggestion not to use a whole column as reference.
来源:https://stackoverflow.com/questions/58283491/how-to-accept-9-first-characters-in-excel-formula