Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 当语句变为False时If语句失败_Excel_Vba_If Statement_Runtime Error - Fatal编程技术网

Excel 当语句变为False时If语句失败

Excel 当语句变为False时If语句失败,excel,vba,if-statement,runtime-error,Excel,Vba,If Statement,Runtime Error,晚上我创建了一个子函数。但是,在它的第一个实例中,如果在它运行if和I之后删除一个值并使if语句为False,那么它就可以工作。它出现调试对话运行时错误“13”(类型不匹配) 我已经运行了很多次,它与VBA代码中的其他If语句类似,但这是唯一一个失败的语句。我只有有限的VBA编程知识,所以在这个问题上的任何帮助将不胜感激 Private Sub BenifitCheck() If Not IsEmpty(TB_HoursSaved.Value) And Not IsEmpty(TB_TimeA

晚上我创建了一个子函数。但是,在它的第一个实例中,如果在它运行if和I之后删除一个值并使if语句为False,那么它就可以工作。它出现调试对话运行时错误“13”(类型不匹配)

我已经运行了很多次,它与VBA代码中的其他If语句类似,但这是唯一一个失败的语句。我只有有限的VBA编程知识,所以在这个问题上的任何帮助将不胜感激

Private Sub BenifitCheck()

If Not IsEmpty(TB_HoursSaved.Value) And Not IsEmpty(TB_TimeAllocation.Value) And Not 
  TB_HoursSaved.Value = "" And Not TB_TimeAllocation.Value = "" And TB_HoursSaved.Value / 
  TB_TimeAllocation.Value > 10 Then
  Let TB_Result = "PASSED"
  TB_Result.ForeColor = RGB(84, 130, 53)
  TB_BenifitRatio.BackColor = RGB(146, 208, 80)
  TB_BenifitRatio.ForeColor = RGB(0, 0, 0)
  ElseIf Not IsEmpty(TB_HoursSaved.Value) And Not IsEmpty(TB_TimeAllocation.Value) And Not 
  TB_HoursSaved.Value = "" And Not TB_TimeAllocation.Value = "" And TB_HoursSaved.Value / 
  TB_TimeAllocation.Value >= 5 And TB_HoursSaved.Value / TB_TimeAllocation.Value < 10 Then
  Let TB_Result = "REVIEW"
  TB_Result.ForeColor = RGB(255, 192, 0)
  TB_BenifitRatio.BackColor = RGB(255, 192, 0)
  TB_BenifitRatio.ForeColor = RGB(0, 0, 0)
  ElseIf Not IsEmpty(TB_HoursSaved.Value) And Not IsEmpty(TB_TimeAllocation.Value) And Not 
  TB_HoursSaved.Value = "" And Not TB_TimeAllocation.Value = "" And TB_HoursSaved.Value / 
  TB_TimeAllocation.Value < 5 Then
  Let TB_Result = "FAIL"
  TB_Result.ForeColor = RGB(255, 0, 0)
  TB_BenifitRatio.BackColor = RGB(255, 0, 0)
  TB_BenifitRatio.ForeColor = RGB(255, 255, 255)
  Else
  TB_Result = ""
  TB_BenifitRatio.BackColor = RGB(255, 255, 255)
  TB_BenifitRatio.ForeColor = RGB(0, 0, 0)

On Error Resume Next
Call Error

End If

End Sub
Private Sub BenifitCheck()
如果不是IsEmpty(TB_HoursSaved.Value)和非IsEmpty(TB_TimeAllocation.Value)且不是
TB_hours saved.Value=“”而不是TB_TimeAllocation.Value=“”和TB_hours saved.Value/
TB_TimeAllocation.Value>10然后
让TB_Result=“通过”
TB_Result.ForeColor=RGB(84130,53)
TB_BenifitRatio.BackColor=RGB(146、208、80)
TB_BenifitRatio.ForeColor=RGB(0,0,0)
ElseIf Not IsEmpty(TB_HoursSaved.Value)和Not IsEmpty(TB_TimeAllocation.Value)以及Not
TB_hours saved.Value=“”而不是TB_TimeAllocation.Value=“”和TB_hours saved.Value/
TB_TimeAllocation.Value>=5,TB_hours saved.Value/TB_TimeAllocation.Value<10
让TB_Result=“查看”
TB_Result.ForeColor=RGB(255,192,0)
TB_BenifitRatio.BackColor=RGB(255,192,0)
TB_BenifitRatio.ForeColor=RGB(0,0,0)
ElseIf Not IsEmpty(TB_HoursSaved.Value)和Not IsEmpty(TB_TimeAllocation.Value)以及Not
TB_hours saved.Value=“”而不是TB_TimeAllocation.Value=“”和TB_hours saved.Value/
TB_TimeAllocation.Value<5则
让TB_Result=“失败”
TB_Result.ForeColor=RGB(255,0,0)
TB_BenifitRatio.BackColor=RGB(255,0,0)
TB_BenifitRatio.ForeColor=RGB(255、255、255)
其他的
TB_Result=“”
TB_BenifitRatio.BackColor=RGB(255、255、255)
TB_BenifitRatio.ForeColor=RGB(0,0,0)
出错时继续下一步
呼叫错误
如果结束
端接头

感谢您的帮助,根据您的建议,我已经获得了代码:

Private Sub BenifitCheck()

If Not IsEmpty(TB_HoursSaved.Value) And Not IsEmpty(TB_TimeAllocation.Value) And Not 
TB_HoursSaved.Value = "" And Not TB_TimeAllocation.Value = "" Then

    If (TB_HoursSaved.Value / TB_TimeAllocation.Value) > 10 Then
    Let TB_Result = "PASSED"
    TB_Result.ForeColor = RGB(84, 130, 53)
    TB_BenifitRatio.BackColor = RGB(146, 208, 80)
    TB_BenifitRatio.ForeColor = RGB(0, 0, 0)

    ElseIf (TB_HoursSaved.Value / TB_TimeAllocation.Value) >= 5 And (TB_HoursSaved.Value / TB_TimeAllocation.Value) < 10 Then
    Let TB_Result = "REVIEW"
    TB_Result.ForeColor = RGB(255, 192, 0)
    TB_BenifitRatio.BackColor = RGB(255, 192, 0)
    TB_BenifitRatio.ForeColor = RGB(0, 0, 0)

    ElseIf (TB_HoursSaved.Value / TB_TimeAllocation.Value) < 5 Then
    Let TB_Result = "FAIL"
    TB_Result.ForeColor = RGB(255, 0, 0)
    TB_BenifitRatio.BackColor = RGB(255, 0, 0)
    TB_BenifitRatio.ForeColor = RGB(255, 255, 255)

    ElseIf TB_BenifitRatio.Value = "" Then
    TB_Result = ""
    TB_BenifitRatio.BackColor = RGB(255, 255, 255)
    TB_BenifitRatio.ForeColor = RGB(0, 0, 0)

    End If

Else: Let TB_Result = ""
TB_BenifitRatio.BackColor = RGB(255, 255, 255)
TB_BenifitRatio.ForeColor = RGB(0, 0, 0)

End If

End Sub
Private Sub BenifitCheck()
如果不是IsEmpty(TB_HoursSaved.Value)和非IsEmpty(TB_TimeAllocation.Value)且不是
TB_HoursSaved.Value=“”而不是TB_TimeAllocation.Value=“”,然后
如果(TB_HoursSaved.Value/TB_TimeAllocation.Value)>10,则
让TB_Result=“通过”
TB_Result.ForeColor=RGB(84130,53)
TB_BenifitRatio.BackColor=RGB(146、208、80)
TB_BenifitRatio.ForeColor=RGB(0,0,0)
ElseIf(TB_hours saved.Value/TB_TimeAllocation.Value)>=5和(TB_hours saved.Value/TB_TimeAllocation.Value)<10则
让TB_Result=“查看”
TB_Result.ForeColor=RGB(255,192,0)
TB_BenifitRatio.BackColor=RGB(255,192,0)
TB_BenifitRatio.ForeColor=RGB(0,0,0)
ElseIf(TB_hours saved.Value/TB_TimeAllocation.Value)<5然后
让TB_Result=“失败”
TB_Result.ForeColor=RGB(255,0,0)
TB_BenifitRatio.BackColor=RGB(255,0,0)
TB_BenifitRatio.ForeColor=RGB(255、255、255)
ElseIf TB_BenifitRatio.Value=”“然后
TB_Result=“”
TB_BenifitRatio.BackColor=RGB(255、255、255)
TB_BenifitRatio.ForeColor=RGB(0,0,0)
如果结束
否则:让TB_Result=“”
TB_BenifitRatio.BackColor=RGB(255、255、255)
TB_BenifitRatio.ForeColor=RGB(0,0,0)
如果结束
端接头

您可以通过更仔细地考虑您想用sub实现的目标来大大简化代码。例如,您正在进行检查,如果检查失败,您希望引发错误

这样的操作非常常见,通常最好通过将检查封装在自己的函数中来实现,这样您就可以编写类似“如果没有检查通过,则执行错误条件”的代码

所以我会用下面的方法重写你的潜艇

Sub Test()

    If Not BenefitCheckPasses Then Err.raise 17

    ' Code for successful benefit check
End Sub

Private Function BenefitCheckPasses() As Boolean

    BenefitCheckPasses = False

    If IsEmpty(TB_HoursSaved.Value) Then Exit Function
    If IsEmpty(TB_TimeAllocation.Value) Then Exit Function
    If TB_HoursSaved.Value = "" Then Exit Function
    If TB_TimeAllocation.Value = "" Then Exit Function

    BenefitCheckPasses = True

    Dim Ratio As Double
    Ratio = TB_HoursSaved.Value / TB_TimeAllocation.Value

    Select Case Ratio

        Case Is > 10

            TB_Result = "PASSED"
            TB_Result.ForeColor = RGB(84, 130, 53)
            TB_BenifitRatio.BackColor = RGB(146, 208, 80)
            TB_BenifitRatio.ForeColor = RGB(0, 0, 0)

        Case Is > 5

            TB_Result = "REVIEW"
            TB_Result.ForeColor = RGB(255, 192, 0)
            TB_BenifitRatio.BackColor = RGB(255, 192, 0)
            TB_BenifitRatio.ForeColor = RGB(0, 0, 0)

        Case Else

            Let TB_Result = "FAIL"
            TB_Result.ForeColor = RGB(255, 0, 0)
            TB_BenifitRatio.BackColor = RGB(255, 0, 0)
            TB_BenifitRatio.ForeColor = RGB(255, 255, 255)

    End Select

End Function

正如您所看到的,您有一个else子句,它完全不相关,也不可执行。

哪一行抛出了错误?它突出显示了第一行。当我尝试单步执行时,它保持在这一行,但该语句最初是有效的。当它变成假的时候,它就失败了。我试过了,但在最初运行之后仍然失败了。有一件事我没有尝试过,就是如果IsEmpty(TB…Value),并将其嵌套在Else函数中<代码>。。。和(TB\u HoursSaved.Value/TB\u TimeAllocation.Value>10)。如果这不起作用,那么隔离第一行中的哪个确切语句失败。在调试窗口中逐个尝试它们。您很可能会这样理解这个问题。我已经将代码修改为嵌套if,直到我将该语句设置为false为止,它仍然有效。这是我认为失败的一行-如果(TB_hoursvaved.Value/TB_TimeAllocation.Value)>10,则不再需要
Let