简体   繁体   中英

How to do an if statement

This question is linked to Formula to remove every middle name in a cell in Excel .

I basically want to make an if else statement in excel. So the IF checks if the cell is equal to "Gian" OR "Pier", if the condition is confirmed the formula proceeds to use this other formula

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

Sorry guys idk how to do it in an excel way. I can show you in for example in a Java or C way.

if(A2=="Pier" || A2=="Gian")
       =IFERROR(LEFT(A2,FIND(" ",A2)-1),A2) \\the excel formula that deletes every second/third name if the cell

在此处输入图像描述

if formula in excel that checks a condition and if its verified it proceeds to use another excel formula

You could try the following as per your Excel Versions

在此处输入图像描述


• Formula used in cell B2

=IF(OR(TEXTBEFORE(A2&" "," ")={"Pier","Gian"}),A2,TEXTBEFORE(A2&" "," "))

Or, in cell C2

=IF(OR(LEFT(A2&" ",FIND(" ",A2&" ")-1)={"Pier","Gian"}),A2,LEFT(A2&" ",FIND(" ",A2&" ")-1))

Just adding the use of LET() which makes it simpler,

在此处输入图像描述


• Formula used in cell B2

=LET(x,TEXTBEFORE(A2&" "," "),IF(OR(x={"Pier","Gian"}),A2,x))

Or, Formula used in cell C2

=LET(x,LEFT(A2&" ",FIND(" ",A2&" ")-1),IF(OR(x={"Pier","Gian"}),A2,x))

Using MAP() to Spill as one dynamic array formula but the logic remains same.

在此处输入图像描述


• Formula used in cell D2

=MAP(A2:A6,LAMBDA(m,
LET(x,TEXTBEFORE(m&" "," "),
IF(OR(x={"Pier","Gian"}),m,x))))

you have to use the AND(...) and OR(..) for chaining logical conditions.

Here's an example

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