一些excel IF语句可能会变得相当长,我正在寻找一种更简单的方法来编写它们。例如,如果我要写:
If($B$4+13=7,$B$4+13,FALSE)我认为这样做会更容易:
If($B$4+13=7,[Do the left hand side of the equation without making me re-type it], FALSE)特别是当条件很长很复杂的时候。
有没有什么我可以写的东西来代替:
[Do the left hand side of the equation without making me re-type it]谢谢!
发布于 2016-01-23 05:04:54
好吧,为什么不这么做呢
=If($B$4+13=7,7,FALSE)
比较分为两部分。您知道要与之进行比较的是什么,因此无需再次编写公式,只需在真部分中使用比较值即可。
编辑:简化包含重复公式的冗长、复杂的IF语句的另一种方法:
=IF(A1="x",<complex formula>*100,<complex formula>*200)重写到
=<complex formula>*if(A1="x",100,200)发布于 2016-01-23 05:27:16
Jeeped建议使用UDF,所以我想我应该试一试:
Function SUPERIF(LeftCondition, RightCondition, Optional Operator = "EQUALS", Optional Side = "LEFT")
Dim Result
If (Side = "LEFT") Then
Result = LeftCondition
ElseIf (Side = "RIGHT") Then
Result = RightCondition
Else
SUPERIF = xlErrValue
End If
If (Operator = "EQUALS") Then
If (LeftCondition = RightCondition) Then
SUPERIF = Result
Else
SUPERIF = False
End If
End If
If (Operator = "GREATER") Then
If (LeftCondition > RightCondition) Then
SUPERIF = Result
Else
SUPERIF = False
End If
End If
If (Operator = "GRTEQL") Then
If (LeftCondition >= RightCondition) Then
SUPERIF = Result
Else
SUPERIF = False
End If
End If
If (Operator = "LESS") Then
If (LeftCondition < RightCondition) Then
SUPERIF = Result
Else
SUPERIF = False
End If
End If
If (Operator = "LESSEQL") Then
If (LeftCondition <= RightCondition) Then
SUPERIF = Result
Else
SUPERIF = False
End If
End If
If (Operator = "NOT") Then
If (LeftCondition <> RightCondition) Then
SUPERIF = Result
Else
SUPERIF = False
End If
End If
End Function这将作为如下公式输入:=SUPERIF(A1, B1, "GREATER", "RIGHT"),它将测试A1是否大于B1,如果大于,则返回B1的值。
我把最后两个参数设为可选的,默认值是"EQUALS“和" left ",所以你可以只写SUPERIF([some formula], [some other formula]),只要两者的结果是相同的,那么if就会返回左边的值。对于您提供的示例,应将其编写为=SUPERIF($B$4+13,7)
请注意,如果您使用UDF,则需要在启用宏的情况下保存工作簿(*.xlsm)。
https://stackoverflow.com/questions/34954722
复制相似问题