我正在研究一个细长的IF语句,根据零件号的开头产生不同的输出,例如: 3XXXXXXX = Software
,4XXXXXXX = Hardware
。
我遇到的问题是,当部件编号尚未提供给行时,公式会输出FALSE
。
=IF(B2>0,IF(LEFT(D2,1)="1","Assembly",IF(LEFT(D2,1)="2","Sub-Assembly",IF(LEFT(D2,1)="3","Software",IF(LEFT(D2,1)="4","Hardware",IF(LEFT(D2,1)="5","Chemical",IF(LEFT(D2,1)="6","Spare",IF(LEFT(D2,1)="7","Spare",IF(LEFT(D2,1)="8","Document",IF(LEFT(D2,1)="9","Misc",""))))))))))
如果你能找到我错的地方,请告诉我!
答案 0 :(得分:3)
您至少还有两个其他选项,它们都为您提供了更好的可维护代码。
如果您使用Excel 2016或更高版本,则有SWITCH
功能。您的公式将更具可读性:
=IF(B2>0,SWITCH(LEFT(D2,1),"1","Assembly","2","Sub-Assembly","3","Software","4","Hardware","5","Chemical","6","Spare","7","Spare","8","Document","9","Misc",""))
即使在早期的Excel版本中,您也可以使用查找表,它为您提供了更高的通用性。创建一个类别表,您可以将它们放在单独的表Categories
:
使用VLOOKUP
功能与IFERROR
一起提供未找到时的默认值:
=IFERROR(VLOOKUP(LEFT(D2,1),Categories!$A$1:$B$9,2,FALSE),"")
答案 1 :(得分:2)
您没有为第一个大条件设置任何值(如果B2不是> 0)
请改为尝试:
=IF(B2>0,IF(LEFT(D2,1)="1","Assembly",IF(LEFT(D2,1)="2","Sub-Assembly",IF(LEFT(D2,1)="3","Software",IF(LEFT(D2,1)="4","Hardware",IF(LEFT(D2,1)="5","Chemical",IF(LEFT(D2,1)="6","Spare",IF(LEFT(D2,1)="7","Spare",IF(LEFT(D2,1)="8","Document",IF(LEFT(D2,1)="9","Misc",""))))))))),"")
答案 2 :(得分:2)
您可以像这样简化公式:
=IF(B2<=0,"",CHOOSE(LEFT(D2,1),"Assembly","Sub-Assembly","Software","Hardware",
"Chemical","Spare","Spare","Document","Misc"))