Combining 6 IF formulas - excel

I've written 6 different IF formulas, each will identify a freight carrier based on the tracking number found in cell BM71.
For the life of me I cannot figure out how to combine these, any help would be appreciated.
=IF(AND(LEN(BM71)=18,LEFT(BM71,2)="1Z"), "UPS", "")
=IF(AND(LEN(BM71)=12,ISNUMBER(BM71)),"FedEx","")
=IF(AND(LEN(BM71)=10,ISNUMBER(BM71)),"DHL","")
=IF(AND(LEN(BM71)=11,LEFT(BM71,2)="06"), "Old Dominion", "")
=IF(AND(LEN(BM71)=9,LEFT(BM71,2)="00"), "Arcbest", "")
=IF(AND(LEN(BM71)=10,LEFT(BM71,2)="00"), "Averitt", "")

With ifS function it gets more clean:
=IFs(AND(LEN(BM71)=18,LEFT(BM71,2)="1Z"), "UPS",
AND(LEN(BM71)=12,ISNUMBER(BM71)),"FedEx",
AND(LEN(BM71)=10,ISNUMBER(BM71)),"DHL",
AND(LEN(BM71)=11,LEFT(BM71,2)="06"), "Old Dominion",
AND(LEN(BM71)=9,LEFT(BM71,2)="00"), "Arcbest",
AND(LEN(BM71)=10,LEFT(BM71,2)="00"), "Averitt",
True,"")

You just add a new IF in the False part. Like this:
=IF(AND(LEN(BM71)=18,LEFT(BM71,2)="1Z"), "UPS",
IF(AND(LEN(BM71)=12,ISNUMBER(BM71)),"FedEx",
IF(AND(LEN(BM71)=10,ISNUMBER(BM71)),"DHL",
IF(AND(LEN(BM71)=11,LEFT(BM71,2)="06"), "Old Dominion",
IF(AND(LEN(BM71)=9,LEFT(BM71,2)="00"), "Arcbest",
IF(AND(LEN(BM71)=10,LEFT(BM71,2)="00"), "Averitt", ""))))))

No love for LET?
Something like:
=LET(x,BM71,l,LEN(x),b,ISNUMBER(x),s,LEFT(x,2),IFS(
(l=18)*(s="1Z"),"UPS",
(l=12)*b,"FedEx",
(l=10)*b,"DHL",
(l=11)*(s="06"),"Old Dominion",
(l=9)*(s="00"),"Arcbest",
(l=10)*(s="00"),"Averitt"))
If nothing else it cuts down the formula length, and the function only takes one input cell reference (rather than 12 ...).
EDIT: Though if it were me (as other comments have mentioned) I would use this:
=LET(x,BM71,l,LEN(x),b,ISNUMBER(x),s,LEFT(x,2),c,IFS(
(l=18)*(s="1Z"),1,
(l=12)*b,2,
(l=10)*b,3,
(l=11)*(s="06"),4,
(l=9)*(s="00"),5,
(l=10)*(s="00"),6,
TRUE,7),INDEX(Carriers,c) )
With the range named Carriers holding the list of carriers, with a blank in the last row. Makes it easier to change a carrier name and you keep the list in one place which can be re-used by other formulas. (But then I am a devotee of the Third Normal Form ...)

You can add next one instead of "" of previous formula
=IF(AND(LEN(BM71)=18,LEFT(BM71,2)="1Z"), "UPS", IF(AND(LEN(BM71)=12,ISNUMBER(BM71)),"FedEx",IF(AND(LEN(BM71)=10,ISNUMBER(BM71)),"DHL",IF(AND(LEN(BM71)=11,LEFT(BM71,2)="06"), "Old Dominion", IF(AND(LEN(BM71)=9,LEFT(BM71,2)="00"), "Arcbest", IF(AND(LEN(BM71)=10,LEFT(BM71,2)="00"), "Averitt", ""))))))
However you approach to define something based on length and some chars is not stable
I hope I could help you

Related

2 nest if with different source data excel

I have 2 data sources and i want to lookup data using Vlookup by using unique ID,
1st data source will show "X" if the ID locate there,
2nd data source will show "Y" if the ID locate there,
the other responses of ID lookup will show nothing ""
Data source 1:
20000543
10056577
10032255
10008287
10011657
10008119
10008212
10007880
10052842
10007959
10062445
10011361
10008079
10008836
10007792
10052651
10007793
10047404
10007913
10007528
10008252
10007388
10007311
10057445
10036263
20004767
20004010
20001276
20010704
10039655
10062427
20010703
10048331
Data source 2:
20003695
20010673
10011463
10021645
10007439
20001123
10008276
10055885
10007718
10007587
10007364
10055257
10007651
10056216
10054797
10052500
10059984
10027674
10010153
10007808
10008182
10007763
10007428
10008102
10050130
10037579
10021909
10009539
10057979
I want to lookup value by using ID "10008102" which is locate in Data source 2 than it will show "Y" for final result,
How can I achieve this?
Much appreciate that.
Thank you
Please don't kill me for the overkill, but I came up with this:
=IF(IFNA(MATCH(A1,Sheet2!A$1:A$29,0),"N")="N","N","Y")
The story:
Match(...,...,0) searches for an exact match (hence the zero).
you need to look for a relative reference, like A1 (without dollarsigns), so that you can drag down and use another cell reference at any time
you need to look inside a fix array, like A$1:A$29, so that, when you drag down, you don't modify the array you're looking in.
Match() either gives a number or an #N/A error in case not found.
IFNA() translates #N/A to "N" (but there's no else-case in IFNA())
IF() has an else-case.
I'm inviting Excel wizards to come up with a simpler solution :-)
Do you possibly want to account for the value existing in both or none of the columns?
=IF(AND(COUNTIF(A:A,E2)>0,COUNTIF(B:B;E2)>0),"X&Y",
IF(COUNTIF(A:A,E2)>0,"X",
IF(COUNTIF(B:B,E2)>0,"Y",
"NONE")))

Error with formula trying to use IF(AND with three conditions

I am trying to write a formula to evaluate all possible values in three cells and score them in another. Excel says that there is a problem with my formula, but I can't locate it. Your help is appreciated.
I am using IF(AND throughout my spreadsheet, but only with two values. Those formulas are accepted by Excel.
=IF(AND(B2="No",B3="Red",B5="No"),0,
IF(AND(B2="No",B3="Green",B5="No",2,
IF(AND(B2="No",B3="Blue",B5="No",3,
IF(AND(B2="No",B3="Yellow",B5="No",5,5,
IF(AND(B2="No",B3="Red",B5="Yes"),0,
IF(AND(B2="No",B3="Green",B5="Yes"),2,
IF(AND(B2="No",B3="Blue",B5="Yes",3,
IF(AND(B2="No",B3="Yellow",B5="Yes",5,
IF(AND(B2="Yes",B3="Red",B5="Yes",0,
IF(AND(B2="Yes",B3="Green",B5="Yes",1,
IF(AND(B2="Yes",B3="Blue",B5="Yes",2,
IF(AND(B2="Yes",B3="Yellow",B5="Yes",5,
IF(AND(B2="Yes",B3="Red",B5="No",0,
IF(AND(B2="Yes",B3="Green",B5="No",1,
IF(AND(B2="Yes",B3="Blue",B5="No",2,
IF(AND(B2="Yes",B3="Yellow",B5="No",3))))))))))))))))
Excel states "There is a problem with this formula".
You had a 5,5 where you should have had a 5 and where missing many ) to close the ANDs
=IF(AND(B2="No",B3="Red",B5="No"),0,
IF(AND(B2="No",B3="Green",B5="No"),2,
IF(AND(B2="No",B3="Blue",B5="No"),3,
IF(AND(B2="No",B3="Yellow",B5="No"),5,
IF(AND(B2="No",B3="Red",B5="Yes"),0,
IF(AND(B2="No",B3="Green",B5="Yes"),2,
IF(AND(B2="No",B3="Blue",B5="Yes"),3,
IF(AND(B2="No",B3="Yellow",B5="Yes"),5,
IF(AND(B2="Yes",B3="Red",B5="Yes"),0,
IF(AND(B2="Yes",B3="Green",B5="Yes"),1,
IF(AND(B2="Yes",B3="Blue",B5="Yes"),2,
IF(AND(B2="Yes",B3="Yellow",B5="Yes"),5,
IF(AND(B2="Yes",B3="Red",B5="No"),0,
IF(AND(B2="Yes",B3="Green",B5="No"),1,
IF(AND(B2="Yes",B3="Blue",B5="No"),2,
IF(AND(B2="Yes",B3="Yellow",B5="No"),3,""))))))))))))))))
But I think you can do this with a simpler formula:
=IFERROR(IF(B2 = "No",CHOOSE(MATCH(B3,{"Red","Green","Blue","Yellow"},0),0,2,3,5),IF(B5="Yes",CHOOSE(MATCH(B3,{"Red","Green","Blue","Yellow"},0),0,1,2,5),CHOOSE(MATCH(B3,{"Red","Green","Blue","Yellow"},0),0,1,2,3))),"")
You could put your logic in string form in one cell (say, F1):
0NOREDNO 2NOGREENNO 3NOBLUENO 5NOYELLOWNO 0NOREDYES 2NOGREENYES 3NOBLUEYES 5NOYELLOWYES 0YESREDYES 1YESGREENYES 2YESBLUEYES 5YESYELLOWYES 0YESREDNO 1YESGREENNO 2YESBLUENO 3YESYELLOWNO
Then your code reduces to:
=MID(F1, FIND(UPPER(B2&B3&B5),F1)-1, 1)
This would also make it extremely easy to add more conditions in the future.
Try this:
=IF(B3="Red",0,
IF(B3="Green",
IF(B2="Yes",1,
IF(B2="No",2,NA())),
IF(B3="Blue",
IF(B2="Yes",2,
IF(B2="No",3,NA())),
IF(B3="Yellow",
IF(AND(B2="Yes",B5="No"),3,5),
NA()))))
Hope that helps

How to extract the characters from a string in Excel

Hi I would like to extract dynamically the numbers from string in Excel.
I have the following strings and I would like to have only the numbers before ". pdf". taken out of the string into the next column.
As you can see the number of characters varies from line to line.
I have invented something like this:
=MID(M20;SEARCH("_";M20);20)
But this takes out only the numbers after "_" and .pdf after this....
How to make it the way I like?
D:\Users\xxxx\Desktop\1610\ts25b_4462.pdf
D:\Users\xxx\Desktop\1610\ts02b_39522.pdf
D:\Users\xxxxx\Desktop\1610\ts02b_except_39511.pdf
D:\Users\xxxx\Desktop\1610\ts02b_except_39555.pdf
D:\Users\xxxx\Desktop\1610\ts22b_6118.pdf
So that I have just :
4462
39522
39511
39555
6118
and so on...
Thank you!!!
With VBA, try to do it like this:
Public Function splitThings(strInput As String) As String
splitThings = Split(Split(strInput, "_")(1), ".")(0)
End Function
Concerning your formula, try to use =LEFT(MID(M20;SEARCH("_";M20);20),K), where K is the difference of the length of ts22b_6118.pdf and 4 (.pdf). 4 is the length of .pdf.
Something like this should do the work:
=LEFT(MID(I3,SEARCH("_",I3)+1,LEN(I3)),LEN(MID(I3,SEARCH("_",I3),LEN(I3)))-5)
You should do it using Excel formula. For example:
=SUBSTITUTE(LEFT(A1,FIND(".pdf",A1)-1),LEFT(A1,FIND("_",A1)),"")
Using the first line as an example, with LEFT(A1,FIND(".pdf",A1)-1) you will have D:\Users\xxxx\Desktop\1610\ts25b_4462 and with the LEFT(A1,FIND("_",A1)) D:\Users\xxxx\Desktop\1610\ts25b_, if you SUBSTITUTE the first part by "" you will have 4462.
Hope this can help.
With this formula, you should be able to get the numbers you want:
=MID(A1,FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))+1,FIND(".",A1)-FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))-1)
Basically, this is the initial fomula:
=MID(A1,FIND("_",A1)+1,FIND(".",A1)-FIND("_",A1)-1)
But since there may be two _ in the string so this is the one to find the 2nd _:
=SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_","")))
Now just replace this SUBSTITUTE with A1 above and you get that long formula. Hope this helps.
This will return the number you want regardless of extension (could be .pdf, could be .xlsx, etc) and regardless of the number of underscores present in the filename and/or filepath:
=TRIM(LEFT(RIGHT(SUBSTITUTE(SUBSTITUTE(M20,".",REPT(" ",LEN(M20))),"_",REPT(" ",LEN(M20))),LEN(M20)*2),LEN(M20)))

Check to see if cell contains 2 spaces directly after each other

Is there a way that I can check to see if my cell contains 2 spaces immediately after one another?
For example if my cell contained "The cat went meow" then my formula below will return "No" because there is only one space between each character. However, if there where 2 spaces like
"The cat went meow"
then the formula would return "Yes".
I have tried the following formula but it picks up all the spaces instead of what I want it to do.
Can someone please show me how i could correct this?
=IF(ISNUMBER(SEARCH(" " & " ",B1)),"Yes","no")
Might be worth considering TRIM():
=LEN(B1)=LEN(TRIM(B1))
Test cases:
"The cat went meow" TRUE (single space)
"The cat went meow" FALSE (double space)
" The cat went meow" FALSE (leading space)
"The cat went meow " FALSE (trailing space)
this works for your example, if allowance is made for returning T/F rather than Y/N (to keep the formula short - Y/N could be arranged).
A better (shorter) version was offered by #Rick Hitchcock (to whom thanks) in a Comment:
=B1=TRIM(B1)
However it would return FALSE not only for "double" spaces but any quantity of spaces that are not on their own (immediately next to characters on both sides)
and
it would return FALSE even for single spaces if at the start and/or end of your string.
So this is not exactly what you asked for, and no more, but in practice seems likely to be more useful in general.
Your code works for me as-is. You could simplify it like this:
=IF(ISNUMBER(SEARCH(" ",B1)),"Yes","no")
To avoid error-checking, you could compare the string to a version with double-spaces converted to spaces, like this:
=IF(SUBSTITUTE(B1," "," ")<>B1,"Yes","no")
But I'm unsure what problem you're having with your existing code.
Try this
=IF(ISERROR(FIND(" ",B1,1)),"No","Yes")
or
=IF(ISERROR(SEARCH(" ",B1,1)),"No","Yes")

How can I perform a reverse string search in Excel without using VBA?

I have an Excel spreadsheet containing a list of strings. Each string is made up of several words, but the number of words in each string is different.
Using built in Excel functions (no VBA), is there a way to isolate the last word in each string?
Examples:
Are you classified as human? -> human?
Negative, I am a meat popsicle -> popsicle
Aziz! Light! -> Light!
This one is tested and does work (based on Brad's original post):
=RIGHT(A1,LEN(A1)-FIND("|",SUBSTITUTE(A1," ","|",
LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
If your original strings could contain a pipe "|" character, then replace both in the above with some other character that won't appear in your source. (I suspect Brad's original was broken because an unprintable character was removed in the translation).
Bonus: How it works (from right to left):
LEN(A1)-LEN(SUBSTITUTE(A1," ","")) – Count of spaces in the original string
SUBSTITUTE(A1," ","|", ... ) – Replaces just the final space with a |
FIND("|", ... ) – Finds the absolute position of that replaced | (that was the final space)
Right(A1,LEN(A1) - ... )) – Returns all characters after that |
EDIT: to account for the case where the source text contains no spaces, add the following to the beginning of the formula:
=IF(ISERROR(FIND(" ",A1)),A1, ... )
making the entire formula now:
=IF(ISERROR(FIND(" ",A1)),A1, RIGHT(A1,LEN(A1) - FIND("|",
SUBSTITUTE(A1," ","|",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))))
Or you can use the =IF(COUNTIF(A1,"* *") syntax of the other version.
When the original string might contain a space at the last position add a trim function while counting all the spaces: Making the function the following:
=IF(ISERROR(FIND(" ",B2)),B2, RIGHT(B2,LEN(B2) - FIND("|",
SUBSTITUTE(B2," ","|",LEN(TRIM(B2))-LEN(SUBSTITUTE(B2," ",""))))))
This is the technique I've used with great success:
=TRIM(RIGHT(SUBSTITUTE(A1, " ", REPT(" ", 100)), 100))
To get the first word in a string, just change from RIGHT to LEFT
=TRIM(LEFT(SUBSTITUTE(A1, " ", REPT(" ", 100)), 100))
Also, replace A1 by the cell holding the text.
A more robust version of Jerry's answer:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))), LEN(TRIM(A1))))
That works regardless of the length of the string, leading or trailing spaces, or whatever else and it's still pretty short and simple.
I found this on google, tested in Excel 2003 & it works for me:
=IF(COUNTIF(A1,"* *"),RIGHT(A1,LEN(A1)-LOOKUP(LEN(A1),FIND(" ",A1,ROW(INDEX($A:$A,1,1):INDEX($A:$A,LEN(A1),1))))),A1)
[edit] I don't have enough rep to comment, so this seems the best place...BradC's answer also doesn't work with trailing spaces or empty cells...
[2nd edit] actually, it doesn't work for single words either...
=RIGHT(TRIM(A1),LEN(TRIM(A1))-FIND(CHAR(7),SUBSTITUTE(" "&TRIM(A1)," ",CHAR(7),
LEN(TRIM(A1))-LEN(SUBSTITUTE(" "&TRIM(A1)," ",""))+1))+1)
This is very robust--it works for sentences with no spaces, leading/trailing spaces, multiple spaces, multiple leading/trailing spaces... and I used char(7) for the delimiter rather than the vertical bar "|" just in case that is a desired text item.
This is very clean and compact, and works well.
{=RIGHT(A1,LEN(A1)-MAX(IF(MID(A1,ROW(1:999),1)=" ",ROW(1:999),0)))}
It does not error trap for no spaces or one word, but that's easy to add.
Edit:
This handles trailing spaces, single word, and empty cell scenarios. I have not found a way to break it.
{=RIGHT(TRIM(A1),LEN(TRIM(A1))-MAX(IF(MID(TRIM(A1),ROW($1:$999),1)=" ",ROW($1:$999),0)))}
=RIGHT(A1,LEN(A1)-FIND("`*`",SUBSTITUTE(A1," ","`*`",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
New answer 9/28/2022
Considering the new excel function: TEXTAFTER (check availability) you can achieve it with a simple formula:
=TEXTAFTER(A1," ", -1)
To add to Jerry and Joe's answers, if you're wanting to find the text BEFORE the last word you can use:
=TRIM(LEFT(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))), LEN(SUBSTITUTE(TRIM(A1), " ", REPT(" ", LEN(TRIM(A1)))))-LEN(TRIM(A1))))
With 'My little cat' in A1 would result in 'My little' (where Joe and Jerry's would give 'cat'
In the same way that Jerry and Joe isolate the last word, this then just gets everything to the left of that (then trims it back)
Copy into a column, select that column and HOME > Editing > Find & Select, Replace:
Replace All.
There is a space after the asterisk.
Imagine the string could be reversed. Then it is really easy. Instead of working on the string:
"My little cat" (1)
you work with
"tac elttil yM" (2)
With =LEFT(A1;FIND(" ";A1)-1) in A2 you get "My" with (1) and "tac" with (2), which is reversed "cat", the last word in (1).
There are a few VBAs around to reverse a string. I prefer the public VBA function ReverseString.
Install the above as described. Then with your string in A1, e.g., "My little cat" and this function in A2:
=ReverseString(LEFT(ReverseString(A1);IF(ISERROR(FIND(" ";A1));
LEN(A1);(FIND(" ";ReverseString(A1))-1))))
you'll see "cat" in A2.
The method above assumes that words are separated by blanks. The IF clause is for cells containing single words = no blanks in cell. Note: TRIM and CLEAN the original string are useful as well. In principle it reverses the whole string from A1 and simply finds the first blank in the reversed string which is next to the last (reversed) word (i.e., "tac "). LEFT picks this word and another string reversal reconstitutes the original order of the word (" cat"). The -1 at the end of the FIND statement removes the blank.
The idea is that it is easy to extract the first(!) word in a string with LEFT and FINDing the first blank. However, for the last(!) word the RIGHT function is the wrong choice when you try to do that because unfortunately FIND does not have a flag for the direction you want to analyse your string.
Therefore the whole string is simply reversed. LEFT and FIND work as normal but the extracted string is reversed. But his is no big deal once you know how to reverse a string. The first ReverseString statement in the formula does this job.
=LEFT(A1,FIND(IF(
ISERROR(
FIND("_",A1)
),A1,RIGHT(A1,
LEN(A1)-FIND("~",
SUBSTITUTE(A1,"_","~",
LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))
)
)
)
),A1,1)-2)
I translated to PT-BR, as I needed this as well.
(Please note that I've changed the space to \ because I needed the filename only of path strings.)
=SE(ÉERRO(PROCURAR("\",A1)),A1,DIREITA(A1,NÚM.CARACT(A1)-PROCURAR("|", SUBSTITUIR(A1,"\","|",NÚM.CARACT(A1)-NÚM.CARACT(SUBSTITUIR(A1,"\",""))))))
Another way to achieve this is as below
=IF(ISERROR(TRIM(MID(TRIM(D14),SEARCH("|",SUBSTITUTE(TRIM(D14)," ","|",LEN(TRIM(D14))-LEN(SUBSTITUTE(TRIM(D14)," ","")))),LEN(TRIM(D14))))),TRIM(D14),TRIM(MID(TRIM(D14),SEARCH("|",SUBSTITUTE(TRIM(D14)," ","|",LEN(TRIM(D14))-LEN(SUBSTITUTE(TRIM(D14)," ","")))),LEN(TRIM(D14)))))
You can achieve this also by reversing the string and finding the first space
=MID(C3,2+LEN(C3)-SEARCH(" ",CONCAT(MID(C3,SEQUENCE(LEN(C3),,LEN(C3),-1),1))),LEN(A1))
Reverse the string
CONCAT(MID(C3,SEQUENCE(LEN(C3),,LEN(C3),-1),1))
Find the first space in the reversed string
SEARCH(" ",...
Take the position of the space found in the reversed string off the length of the string and return that portion
=MID(C3,2+LEN(C3)-SEARCH...
I also had a task like this and when I was done, using the above method, a new method occured to me: Why don't you do this:
Reverse the string ("string one" becomes "eno gnirts").
Use the good old Find (which is hardcoded for left-to-right).
Reverse it into readable string again.
How does this sound?

Resources