简体   繁体   English

改进Excel公式中IF的重复使用

[英]Improving repetitive use of IFs in Excel formula

Is there a more efficient approach to this formula? 有没有更有效的方法来解决这个问题?

=IF(A1="Texas",1,)&IF(A1="Washington",2,)&IF(A1="Kansas",3,)&IF(A1="California",3,)

Simple question, but I'm sure there is a better way then the one I've currently got 这个问题很简单,但是我敢肯定有比我目前所拥有的更好的方法

You could use a table in the worksheet and use Vlookup, 您可以在工作表中使用表格并使用Vlookup,

在此处输入图片说明

or do it all in one formula like 或用一个公式完成所有操作

=LOOKUP(A1,{"California","Kansas","Texas","Washington"},{3,3,1,2})

The first array must be sorted ascending. 第一个数组必须升序排列。

Edit: You can catch errors by wrapping the statement into an IFNA() like this: 编辑:您可以通过将语句包装到IFNA()中来捕获错误,如下所示:

=IFNA(LOOKUP(A1,{"California","Kansas","Texas","Washington"},{3,3,1,2}),"not found")

"Shorter" or "more efficient" really depends on your situation. “更短”还是“更有效”实际上取决于您的情况。 The lookup table is the shortest and it's very quick to add new states to the scenario. 查找表是最短的,可以很快速地将新状态添加到方案中。

The Lookup formula is fine if the states and their values are not expected to change. 如果状态及其值不希望更改,则Lookup公式很好。

Hi you could also use "=INDEX(MATCH(),column)" in your formula. 嗨,您也可以在公式中使用“ = INDEX(MATCH(),column)”。 I once read before that it is much faster than using VLookup / HLookup. 我曾经读过,它比使用VLookup / HLookup快得多。

Match returns the row of the matched item.. Match返回匹配项的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM