首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel公式: If condition then do that condition

Excel公式: If condition then do that condition
EN

Stack Overflow用户
提问于 2016-01-23 03:47:16
回答 2查看 419关注 0票数 2

一些excel IF语句可能会变得相当长,我正在寻找一种更简单的方法来编写它们。例如,如果我要写:

代码语言:javascript
复制
If($B$4+13=7,$B$4+13,FALSE)

我认为这样做会更容易:

代码语言:javascript
复制
If($B$4+13=7,[Do the left hand side of the equation without making me re-type it], FALSE)

特别是当条件很长很复杂的时候。

有没有什么我可以写的东西来代替:

代码语言:javascript
复制
[Do the left hand side of the equation without making me re-type it]

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2016-01-23 05:04:54

好吧,为什么不这么做呢

=If($B$4+13=7,7,FALSE)

比较分为两部分。您知道要与之进行比较的是什么,因此无需再次编写公式,只需在真部分中使用比较值即可。

编辑:简化包含重复公式的冗长、复杂的IF语句的另一种方法:

代码语言:javascript
复制
=IF(A1="x",<complex formula>*100,<complex formula>*200)

重写到

代码语言:javascript
复制
=<complex formula>*if(A1="x",100,200)
票数 1
EN

Stack Overflow用户

发布于 2016-01-23 05:27:16

Jeeped建议使用UDF,所以我想我应该试一试:

代码语言:javascript
复制
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)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34954722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档