Excel if else formula fails due to wrong syntex - excel

I have different class name in excel column in character form. I want to convert it to numerical form and add in column C
If B2==N, make it 1
If B2==O, make it 0
If B2==A, make it 2
If B2==~, make it 9
I have written the following excel formula. I have added my formula and error message in image. Can you correct me where I am making mistake
=IF(B2='N',1,if(b2='O',0,if(b2='A',2,if(b2='~',9))))
Update
After adding the formula as per details answer

Personally I would do something like this, using IFS function:
=IFS(B2="N",1,B2="O",0,B2="A",2,B2="~",9)

Within Excel formulas, double quotes are used to denote the start and end of a text string. Numeric should not be within double quotes.
That said, we can use a formula like IF function but we can also avoid using double quotes by creating a table of reference, refer the image below, for alternative solutions - 7 APPROACHES
• Formula used in cell C2
=VLOOKUP($B2,$I$2:$J$5,2,1)
Without Table Of Reference the above formula can be written as
=VLOOKUP($B2,{"~",9;"A",2;"N",1;"O",0},2,1)
• Formula used in cell D2
=LOOKUP($B2,$I$2:$J$5)
Without Table Of Reference the above formula can be written as
=LOOKUP($B2,{"~",9;"A",2;"N",1;"O",0})
• Formula used in cell E2 --> "Correction To Your Own Formula"
=IF(B2="N",1,IF(B2="O",0,IF(B2="A",2,IF(B2="~",9))))
• Formula used in cell F2
=IFS(B2="~",9,B2="A",2,B2="N",1,B2="O",0)
• Formula used in cell G2
=SWITCH($B2,"~",9,"A",2,"N",1,"O",0)
Note the formulas used in cell F2 & G2 you need to have either Excel 2019, Excel 2021 Or O365 hence its better to use a Table Of Reference to avoid using Double Quotes.
So, if there is a space before each characters then the formula, shall change refer the image below and & the formulas,
• Formula used in cell C2
=VLOOKUP(TRIM($B2),$I$2:$J$5,2,1)
• Formula used in cell D2
=LOOKUP(TRIM($B2),$I$2:$J$5)
• Formula used in cell E2
=IF(TRIM($B2)="N",1,IF(TRIM($B2)="O",0,IF(TRIM($B2)="A",2,IF(TRIM($B2)="~",9))))
• Formula used in cell F2
=IFS(TRIM($B2)="~",9,TRIM($B2)="A",2,TRIM($B2)="N",1,TRIM($B2)="O",0)
• Formula used in cell G2
=SWITCH(TRIM($B2),"~",9,"A",2,"N",1,"O",0)
And if those are not spaces and actually are non printable characters then just change the TRIM to SUBSTITUTE Function
C2
=VLOOKUP(SUBSTITUTE($B2,CHAR(160),""),$I$2:$J$5,2,1)
D2
=LOOKUP(SUBSTITUTE($B2,CHAR(160),""),$I$2:$J$5)
E2
=IF(SUBSTITUTE($B2,CHAR(160),"")="N",1,IF(SUBSTITUTE($B2,CHAR(160),"")="O",0,IF(SUBSTITUTE($B2,CHAR(160),"")="A",2,IF(SUBSTITUTE($B2,CHAR(160),"")="~",9))))
F2
=IFS(SUBSTITUTE($B2,CHAR(160),"")="~",9,SUBSTITUTE($B2,CHAR(160),"")="A",2,SUBSTITUTE($B2,CHAR(160),"")="N",1,SUBSTITUTE($B2,CHAR(160),"")="O",0)
G2
=SWITCH(SUBSTITUTE($B2,CHAR(160),""),"~",9,"A",2,"N",1,"O",0)

In Excel, double quotes is used to enclose character values not single quote. Hence, your formula will become
=IF(B2="N",1,IF(B2="O",0,IF(B2="A",2,IF(B2="~",9,""))))
Additionally, I have added an else clause with blanks in last IF.
Also, if you are on newer versions of Excel, then you can also use SWITCH which is easier and shorter
=SWITCH(B2,"N",1,"O",0,"A",2,"~",9,"")

Related

need to count only numbers after underscore symbol in excel

I need to check if numbers are there after underscore symbol and put that numbers count as shown below.
I am very new to excel, please how to write a formula for this.
Thank you so much in advance
Perhaps you can try something like this. Not sure about your Excel Version.
• Formula used in cell B1
=REPLACE(A1,FIND("#",SUBSTITUTE(A1,"_","#",5)),255,
"_\d{"&LEN(-LOOKUP(0,-RIGHT(A1,ROW($ZY$1:INDEX($Z:$Z,LEN(A1))))))&"}")
If you have access to MS365 then you could try as below,
• Formula used in cell C1
=LET(x,TEXTSPLIT(A1,"_"),
c,LEN(TAKE(x,,-1)),
TEXTJOIN("_",,DROP(x,,-1),"\d{"&c&"}"))
I tried made it short in MS365 version
• Formula used in cell D1
=TEXTBEFORE(A1,"_",-1)&"_\d{"&LEN(TAKE(TEXTSPLIT(A1,"_"),,-1))&"}"
With One Spill Array Formula.
• Formula used in cell D1
=MAP(A1:A3,LAMBDA(m,
TEXTBEFORE(m,"_",-1)&"_\d{"&LEN(TAKE(TEXTSPLIT(m,"_"),,-1))&"}"))
I make the assumption that the characters "cm_" are always before the number:
=len(mid(A1,find("cm_",A1,1)+3,30))
The len() counts the number of characters,
mid() gets the numbers occurring after the text defined in find()
Then:
LEFT(A1,FIND("cm_",A1,1)+2)&"\d{"&LEN(MID(A1,FIND("cm_",A1,1)+3,30))&"}"
My output is here in cell D2..
=REPLACE(C2,MATCH(TRUE,ISNUMBER(--MID(C2,SEQUENCE(LEN(C2)),1)),0),LEN(C2),"\d{"&SUMPRODUCT((ISNUMBER(--MID(C2,SEQUENCE(LEN(C2)),1))*1))&"}")
if your excel doesn't support for Sequence function then you can use ROW(INDIRECT("1:"&LEN(C2)) instead..

Excel formula to lookup a table from another table with comma separated output

I am trying to get a formula that looks up information from another table and populates the output with comma separated values. As shown below, I want to populate column D in Table 2 with information from Table 1. The desired output is in column E. I came up with this formula but it's only pulling one city per person.
TEXTJOIN(", ",,INDEX('Table 1'!B:B,MATCH(FILTERXML(""&SUBSTITUTE(C3,",","")&"","//m"),'Table 1'!A:A,0)))
For you reference, you can see the screenshot below,
Solution as posted by Scott Craner Sir using FILTERXML()
• Formula used in cell G8
=TEXTJOIN(", ",,FILTER($B$3:$B$9,ISNUMBER(MATCH($A$3:$A$9,FILTERXML("<m><b>"&SUBSTITUTE(F3,", ","</b><b>")&"</b></m>","//b"),0)),""))
Note: Depending on one's Excel version this may needs to be keyed with CTRL+SHIFT+ENTER instead of Enter when exiting edit mode.
Perhaps, if you are using MS365 then you can use ARRAYTOTEXT() with TEXTSPLIT() as well,
• Formula used in cell G3
=ARRAYTOTEXT(FILTER($B$3:$B$9,ISNUMBER(MATCH($A$3:$A$9,TEXTSPLIT(F3,,", ",1),0)),""))
Kindly change the cell range/sheet preferences as per your suit.
Edit
Thanks! It appears to be working now. However, there is one down side that I am hoping you can help with. If there is only one person listed, it's returning a #calc error. This formula only appear to be working when there are multiple staff present. Any idea on how to fix this?
• Formula used in cell G8
=TEXTJOIN(", ",,FILTER(B:B,ISNUMBER(MATCH(A:A,FILTERXML("<m><b>"&SUBSTITUTE(F8&", ",", ","</b><b>")&"</b></m>","//b"),0))))
Or,
• Formula used in cell G3
=ARRAYTOTEXT(FILTER($B:$B,ISNUMBER(MATCH($A:$A,TEXTSPLIT(F3&", ",,", ",1),0)),""))
Try:
Formula in D3:
=TEXTJOIN(", ",,IF(ISNUMBER(XMATCH("*, "&A$3:A$9&", *",", "&C3&", ",2)),B$3:B$9,""))
Note I concatenated leading/trailing comma/space's to prevent possible false positives.
Also, in case you wish to avoid duplicates, just nest UNIQUE() in there:
=TEXTJOIN(", ",,UNIQUE(IF(ISNUMBER(XMATCH("*, "&A$3:A$9&", *",", "&C3&", ",2)),B$3:B$9,"")))

Extract part of text in Excel with condition

I know this questions seems redundant but cannot find an answer. I have a text string such as:
A-B-C-D-E-F
I would like to extract only the first 4 items separated by "-". Result should be
A-B-C-D
Alternatively, I am also looking to extract only the 3rd item:
C
I have been trying the MID and SEARCH functions with no success.
Any help would be well appreciated.
Using TEXTSPLIT (As of this writing only available to Office 365 Insider Beta Channel)
=TEXTJOIN("-",TRUE,INDEX(TEXTSPLIT(A1,"-"),1,{1,2,3,4}))
Change the {1,2,3,4} to 3 for the third.
If one does not have TEXTSPLIT but has TEXTJOIN and is on a PC then:
=TEXTJOIN("-",TRUE,INDEX(FILTERXML("<a><b>"&SUBSTITUTE(A1,"-","</b><b>")&"</b></a>","//b"),1,{1,2,3,4}))
Without TEXTJOIN it will require vba. Like this UDF that mimics TEXTJOIN: MS Excel - Concat with a delimiter
So, this is what I have tried,
• Formula used in cell B1
=TEXTJOIN("-",,TAKE(TEXTSPLIT(A1,"-"),,4))
• Formula used in cell D1
=TEXTJOIN("-",,DROP(TEXTSPLIT(A1,"-"),,-2))
• Formula used in cell C1
=TEXTJOIN("-",,INDEX(TEXTSPLIT(A1,"-"),,3))
Note: Formulas shown above works for O365 Users, Insiders Beta Channel users only!
However, if you have access to Excel 2019, then you can use either TEXTJOIN() or CONCAT()
• Formula used in cell B9
=SUBSTITUTE(CONCAT("-"&INDEX(FILTERXML("<t><s>"&SUBSTITUTE(A9,"-","</s><s>")&"</s></t>","//s"),ROW(A1:A4))),"-","",1)
• Formula used in cell C9
=SUBSTITUTE(CONCAT("-"&INDEX(FILTERXML("<t><s>"&SUBSTITUTE(A9,"-","</s><s>")&"</s></t>","//s"),3)),"-","",1)
Since OP has mentioned in comments, that OP is using O365 in MAC, hence here is an update.
• Formula used in cell B1
=TEXTJOIN("-",,TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",100)),COLUMN(A1:D1)*99-98,99)))
• Formula used in cell C1
=TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",100)),COLUMN(C1)*99-98,99))
Or,
• Formula used in cell D1
=TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",100)),200,100))
For Excel MAC and earlier versions:
Extract first four:
=LEFT(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),4))-1)
Extract the third:
=MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),2))+1, FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),3))-FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),2))-1)
Easier to understand (now and in the future) if done in small steps.
Assuming source data is in A2:
in B2: =FIND("-",$A2)
in C2: =FIND("-",$A2,B2+1)
in D2, E2: copy across from C2
Then in F2: =LEFT(A2,E2-1)
You can put all of that into one formula, but that's more error-prone.

Add value to different integers in same cell in MS Excel

I have a typical book index in a spreadsheet that goes like this:
I need Excel to recognize the different integers in the same cell and add a constant each one of them.
Let's say the constant is 5
The result should be like this:
I couldn't get excel to recognize the different values in a cell.
My two cents, assuming Excel ms365:
Formula in B1:
=BYROW(A1:A3,LAMBDA(a,LET(X,TEXTSPLIT(a,", "),TEXTJOIN(", ",,IFERROR(X+C1,X)))))
Or, if no access to TEXTSPLIT(), then use:
=BYROW(A1:A3,LAMBDA(a,LET(X,FILTERXML("<t><s>"&SUBSTITUTE(a,",","</s><s>")&"</s></t>","//s"),TEXTJOIN(", ",,IFERROR(X+C1,X)))))
Or, if no access to BYROW(), you'd have to drag the following:
=LET(X,FILTERXML("<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>","//s"),TEXTJOIN(", ",,IFERROR(X+C$1,X)))
Not sure if this could be done smoothly and without using long and complex formulas.
Why don't you try to split the cell into multiple columns (Data > Data Tools > Text To Columns > Delimited and select Comma as delimiter). Once you have all different values is separate columns, it would be easy to apply formulas and add the constant.
After that, you could use CONCATENATE formula to merge texts and numbers back to single value in a single column.
Hope this helps.
I would separate the text and each value into separate cells, then use & to combine into the format you want, something like so:
=A1&", "&B1+G1&", "&C1+G1&", "&D1+G1
where A1 contains "Trauma" and B1 contains 15, C1 contains 17 and D1 contains 25. G1 holds the constant 5.
See
This is what I have tried,
Formula Applicable To Only O365 Beta Channel (Insiders).
• Formula used in cell B1
=TEXTJOIN(", ",,TEXTBEFORE(A1,", ",1),TEXTSPLIT(TEXTAFTER(A1,", ",1),", ")+5)
Or, if you are not using the above Excel Version, but using either Excel 2019, 2021 or Regular O365, then,
• Formula used in cell B1
=TEXTJOIN(", ",,LEFT(A1,FIND(",",A1)-1),IFERROR(
FILTERXML("<a><b>"&SUBSTITUTE(A1,", ","</b><b>")&"</b></a>","//b")+5,""))
Edit
One improvised approach:
=TEXTJOIN(", ",,TEXTBEFORE(A1,", ",1),IFERROR(TEXTSPLIT(A1,", ")+5,""))
You can also accomplish this task quite easily with Power Query.

Excel: Specify sheetname in another cell for countif formula

My intention is to use the indirect formula. Here is the code I want to edit:
=COUNTIF('sheetname'!R:R;"x")
This code gives a #ref! error:
=INDIRECT("COUNTIF('"&D85&"'!R:R;"&B98&")")
Cell D85 contains sheetname and B98 contains "x".
My version of Excel needs ; in formulas instead of ,.
Thanks in advance for your expertise and time.
Try this formula:
=COUNTIF(INDIRECT("'"&D85&"'!R:R");B98)
My read on Indirect says that it simply uses the cell reference contained in the cell you specify in the function:
Indirect( cellContainingReference )
In this case, you don't need to specify the second parameter of Indirect.
So, using the assumptions:
sheetName is in cell D85
cellRange is always R:R
criteria for counting is in cell B98 (which does not need Indirect to work)
Your excel uses ';' rather than ','
Your formula for CountIf would be:
=countif(indirect("'"&D85&"'!R:R"); B98)
With some modification it works, I hope this is a good compromise for you. The modified formula is
=COUNTIF(INDIRECT(E1); F1)
and in this case that E1 should contain something like
''sheetname'!R:R
(See the double apostrophes at the beginning.)
F1 should contain the "X" or other value you want to count.

Resources