当我执行我的vb6应用程序时,我收到运行时错误6溢出。 我在这一行上收到了这个错误。
Sections("Section5").Controls("lblcap").Caption = Format(((lTotOperMins - KPIDown) / lTotOperMins) * 100, "#0.00")
值lTotOperMins=0 , KPIDown=0 ,
和Data Types
为Long
这段代码有什么问题。建议我,我对VB6并不熟悉。
答案 0 :(得分:4)
正如jac所说,这是因为你不是先检查零除零。我知道这似乎有误导性,因为你没有得到那个错误,你得到了溢出。
至于为什么你得到的而不是零除以不是我醒得思考的东西,而是简单地检查首先修复溢出。下面是一个示例,导致溢出后跟一个没有错误的工作示例:
显示溢出发生位置的图像(无法上传,信誉不足):
Private Sub Form_Load()
Dim lTotOperMins As Long, KPIDown As Long
Dim lonDifference As Long
lTotOperMins = 0
KPIDown = 0
' Prevent / by 0
lonDifference = lTotOperMins - KPIDown
'If 0 = lonDifference Then
'Label1.Caption = Format(0, "#0.00")
'Else
Label1.Caption = Format(((lonDifference) / lTotOperMins) * 100, "#0.00")
'End If
End Sub
Private Sub Form_Load()
Dim lTotOperMins As Long, KPIDown As Long
Dim lonDifference As Long
lTotOperMins = 0
KPIDown = 0
' Prevent / by 0
lonDifference = lTotOperMins - KPIDown
If 0 = lonDifference Then
Label1.Caption = "0.00"
Else
Label1.Caption = Format(((lonDifference) / lTotOperMins) * 100, "#0.00")
End If
End Sub
Private Sub Form_Load()
Dim a As Long, b As Long
a = 0
b = 0
Debug.Print a / b
End Sub