Reputation: 3
i have runtime error '6' overflow when i execute following code.
Private Sub ComboBox1_CLICK()
If val(Label27) >= 0 And val(Label27) <= val(Label9) Then
Label35 = (val(Label13) - val(Label14) + 1) * val(Label27)/val(Label9)
ElseIf val(Label27) >= val(Label9) And val(Label27) <= val(Label6) Then
Label35 = val(Label13) + 1
Else
Label35 = (val(Label13) + 1) * val(Label6) / val(Label27)
End If
end sub
Upvotes: 0
Views: 1744
Reputation: 7891
Sounds like you're trying to divide zero by zero. Check the values of Label9
and Label27
are not zero.
Normally a division by zero will generate a
Run-time error '11': Division by zero
error, but if Label9
is zero (or blank) and (val(Label13) - val(Label14) + 1) * val(Label27)
evaluates to zero (probably because Label27
is zero) you will be calculating 0 / 0
which causes VBA to generate a
Run-time error '6': Overflow
error.
Upvotes: 1
Reputation: 29171
An overflow may happen if VBA is using int
as datatype and an intermediate result exceeds the size of an int
- even if the final result is small enough to fit into an int
Dim i As Integer
i = 1000 * 1000 / 2000 ' Will raise overflow
You should force VBA to use datatype long
.
So instead of using function val
, use function CLng
.
Dim i As Integer
i = CLng(1000) * 1000 / 2000 ' Okay
i = CLng("1000") * CLng("1000") / cLng("2000") ' Okay
As YowEwK statet, this maybe doesn't solve your issue. If not, define a variable for every value you are dealing with (as long
or double
), assign the value f the label to it and check the values of them in the debugger if the runtime error still happens:
Dim val27 As doubble
Dim val19 As doubble
val27 = Val(Label27)
val9 = Val(Label9)
...
BTW: You should consider to name all your fields and variables with something meaningful. And do not forget to add Option explicit
Upvotes: 0