【问题标题】:Below Excel VBA function is only working for first IF condition, ELSEIF is not workingExcel VBA 下面的函数仅适用于第一个 IF 条件,ELSEIF 不起作用
【发布时间】:2021-07-26 22:51:10
【问题描述】:

下面的 Excel VBA 函数仅适用于第一个 If 条件,ElseIf 不起作用。

Function Alarms(L2) As String
    Set a = Application.WorksheetFunction

    If IsError(a.Find("HHH", L2, 1)) = False Then 
        Alarms = "HHH HTRP"
    ElseIf IsError(a.Find(" HI", L2, 1)) <> True Then   
        Alarms = "HI"
    ElseIf IsError(a.Find("IOP", L2, 1)) = False Then   
        Alarms = "IOP"
    ElseIf IsError(a.Find("OOP", L2, 1)) = False Then    
        Alarms = "OOP"
    ElseIf IsError(a.Find("ANS", L2, 1)) = False Then    
        Alarms = "ANS"
    ElseIf IsError(a.Find(" HH", L2, 1)) = False Then    
        Alarms = "HH"
    ElseIf IsError(a.Find("HTRP", L2, 1)) = False Then    
        Alarms = "HHH HTRP"
    ElseIf IsError(a.Find(" LO", L2, 1)) = False Then    
        Alarms = "LO"
    ElseIf IsError(a.Find(" LL", L2, 1)) = False Then    
        Alarms = "LL"
    ElseIf IsError(a.Find("LLL", L2, 1)) = False Then    
        Alarms = "LLL LTRP"
    ElseIf IsError(a.Find("LTRP", L2, 1)) = False Then    
        Alarms = "LLL LTRP"
    ElseIf IsError(a.Find("NR", L2, 1)) = False Then    
        Alarms = "NR"
    ElseIf IsError(a.Find("DV", L2, 1)) = False Then    
        Alarms = "DV"
    ElseIf IsError(a.Find("VEL", L2, 1)) = False Then    
        Alarms = "VEL"
    ElseIf IsError(a.Find("TRIP", L2, 1)) = False Then    
        Alarms = "TRIP"
    ElseIf IsError(a.Find("MHI", L2, 1)) = False Then    
        Alarms = "MHI MLO"
    ElseIf IsError(a.Find("MLO", L2, 1)) = False Then    
        Alarms = "MHI MLO"
    ElseIf IsError(a.Find("CERR", L2, 1)) = False Then    
        Alarms = "CERR"
    ElseIf IsError(a.Find("PERR", L2, 1)) = False Then    
        Alarms = "PERR"
    ElseIf IsError(a.Find("OVR", L2, 1)) = False Then   
        Alarms = "OVR"
    ElseIf IsError(a.Find("FAULT", L2, 1)) = False Then    
        Alarms = "FAULT"
    ElseIf IsError(a.Find("INT", L2, 1)) = False Then    
        Alarms = "INT"
    ElseIf IsError(a.Find("ALM", L2, 1)) = False Then    
        Alarms = "ANN"
    Else
        Alarms = "-"
    End If
End Function

【问题讨论】:

  • 不要使用WorksheetFunction... 使用InStr 并测试结果是否为&gt; 0。此外,创建minimal reproducible example 总是有帮助的。
  • 每当您发现自己使用一长串仅在一个参数上有所不同的If...ElseIf... 语句时,几乎总是有更好的方法来做到这一点。例如,您可以遍历这些字符串的数组。
  • 这是 UDF 吗?如果是,我猜在 UDF 中不允许调用 WorksheetFunction.Find()

标签: excel vba


【解决方案1】:

选择案例可以解决问题:

Public Function Alarms(Target As Range) As String

    Select Case True
        Case InStr(Target.Value, "HHH") > 0, InStr(Target.Value, "HTRP") > 0 '<Note: both values checked here.
            Alarms = "HHH HTRP"
        Case InStr(Target.Value, " HI") > 0
            Alarms = "HI"
        Case InStr(Target.Value, "IOP") > 0
            Alarms = "IOP"
        Case InStr(Target.Value, "OOP") > 0
            Alarms = "OOP"
        Case InStr(Target.Value, "ANS") > 0
            Alarms = "ANS"
        Case InStr(Target.Value, " HH") > 0
            Alarms = "HH"
        Case Else
            Alarms = "-"
    End Select

End Function  

注意 - “OOP”与“oop”不同

【讨论】:

    猜你喜欢
    • 2018-10-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2021-04-28
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    相关资源
    最近更新 更多