【问题标题】:Multiple IF statements without Nested IFs没有嵌套 IF 的多个 IF 语句
【发布时间】:2020-11-13 02:57:27
【问题描述】:

我在一个表中有两列:column_A3 不同的值 a1, a2, a3column_B5 不同的值 b1, b2, b3, b4, b5。我必须根据以下逻辑在 Excel 中创建column_C -

>     if (A=a1)
>         if(B=b1 or B=b2)
>               output=a1_yes
>         else
>               output = a1_no
>     elseif(A=a2)
>         if(B=b3 or B=b4)
>               output = a2_yes
>         else
>               output = a2_no
>     elseif(A=a3)
>         if(B=b5)
>               output = a3_yes
>         else
>               output = a3_no

嵌套if-else 使得逻辑非常复杂。为了简单起见,有没有什么函数可以用来避免嵌套if-else

【问题讨论】:

    标签: excel google-sheets excel-formula excel-2010 excel-2007


    【解决方案1】:

    假设您想摆脱嵌套的if-else,我建议您改用函数 Switch。
    您可以连接您正在验证的列并根据可能的值提供结果。

    鉴于下表:
    制作 1
    模型 1
    颜色 1

    使用公式:

    SWITCH(expression,value1,result1,default_or_value2,result2,...)
    

    示例

    =SWITCH(CONCAT(A1,B1),"Make1","a1_yes","Make2","a1_yes","a2_yes")
    

    参考

    SWITCH Formula

    【讨论】:

    • 谢谢安德烈。您能解释一下为什么在示例中使用了 Concatenate 函数吗?
    • @Neel Concat 函数将为您提供检查嵌套 if 中的所有条件后的最终结果。 示例嵌套 if: if A1 = 'A' if B1 = 'B' or B1 = 'BB' output = 'a1yes' 使用 Concat+Switch 的相同示例: SWITCH(CONCAT(A1,B1), 'AB','a1yes','ABB','a1yes')
    【解决方案2】:

    我认为在这种复杂的情况下,最好创建一个查找表并使用从 Excel 2007 开始工作的 LOOKUP 函数找到所需的值:

    =LOOKUP(2,1/((A2=$H$2:$H$9)*(B2=$I$2:$I$9)+(A2=$H$2:$H$9)*(""=$I$2:$I$9)),$J$2:$J$9)
    

    对于 GS,此公式必须包含在 ARRAYFORMULA 函数中。

    =ArrayFormula(LOOKUP(2,1/((A2=$H$2:$H$9)*(B2=$I$2:$I$9)+(A2=$H$2:$H$9)*(""=$I$2:$I$9)),$J$2:$J$9))
    

    如果您无法在此处创建查找表,则可以选择没有它:

    =LOOKUP(2,
      1/((A2={"a1","a1","a1","a2","a2","a2","a3","a3"})
      *(B2={"","b1","b2","","b3","b4","","b5"})
      +(A2={"a1","a1","a1","a2","a2","a2","a3","a3"})
      *(""={"","b1","b2","","b3","b4","","b5"})),
      {"a1_no","a1_yes","a1_yes","a2_no","a2_yes","a2_yes","a3_no","a3_yes"})
    

    【讨论】:

      猜你喜欢
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多