如果当语句变为假时语句失败
If Statement Failing when Statement Becomes False
晚上我创建了一个子函数。但是,如果在它具有 运行 If 之后,它在第一个实例中起作用,并且我删除了一个值并使 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()
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
您可以通过更加仔细地考虑您希望通过子程序实现的目标来大大简化您的代码。也就是说,您正在进行检查并希望在检查失败时引发错误。
这样的操作很常见,通常最好通过将检查封装在它自己的函数中来实现,这样您就可以编写类似 'if Not Check Passes then do error condition'
的代码
所以我会用下面的方式重写你的子
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 子句。
晚上我创建了一个子函数。但是,如果在它具有 运行 If 之后,它在第一个实例中起作用,并且我删除了一个值并使 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()
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
您可以通过更加仔细地考虑您希望通过子程序实现的目标来大大简化您的代码。也就是说,您正在进行检查并希望在检查失败时引发错误。
这样的操作很常见,通常最好通过将检查封装在它自己的函数中来实现,这样您就可以编写类似 'if Not Check Passes then do error condition'
的代码所以我会用下面的方式重写你的子
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 子句。