我想在Excel中使用多个单元格中的date编写一个嵌套的IF语句

我想有A2 =输出date:

B2 = A2 If B2 ="X" output Date C2 If B2 ="X" and C2="X" output Date D2 If B2 ="X" and C2="X" and D2="X" output Date E2 I tried to write the statement as If(B2="X",C2,IF(C2="X",D2,IFD2="X"E2))) 

Excel布尔链接有时是不直观的,特别是取决于你的背景。 尝试这个:

 =IF(B2 = "X", IF(C2 = "X", IF(D2 = "X", E2, D2), C2), "ERROR") 

这个想法是首先检查最常见的情况,然后在通过时执行额外的检查。 这个代码大致相当于:

  public string ExcelCheck(string b2, string c2, string d2) { if (b2 == "X") { if (c2 == "X") { if (d2 == "X") return "E2"; return "D2"; } return "C2"; } return "ERROR"; } 

你混淆了序列。

如果你这样做

 if(B2 = "X") then return C2; if(B2= "X" && condition2) then return D2; if(B2="X" && condition2 && condition3) then return E2; 

那么它总是会返回C2,因为这是你唯一真正testing的唯一条件。

你需要按照这个顺序来做:

 if(B2="X" && condition2 && condition3) then return E2; if(B2= "X" && condition2) then return D2; if(B2 = "X") then return C2; // TODO: What if none of the conditions match ??? 

所以它需要看起来像这样:

 =IF(AND(B2="X",C2="X",D2="X"),E2,IF(AND(B2="X",C2="X"),D2,IF(B2="X",C2,"Define a default value please in case all conditions are wrong"))) 
Interesting Posts