0

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
11
  • 1
    Sounds like you're trying to divide by zero. Check the values of Label9 and Label27 are not zero. Commented Aug 16, 2017 at 8:26
  • No, division by zero raises runtime error 11 Commented Aug 16, 2017 at 8:40
  • Which line do you get the error on? What are the values of your Label27, etc, variables when it crashes? Commented Aug 16, 2017 at 8:47
  • when i run combobox in userform,the label 27 have not value yet,i think it be equal zero as default. Commented Aug 16, 2017 at 8:57
  • 3
    Ahh - 0 / 0 gives an overflow, so @Olly was probably spot on. So if Label9 and Label27 are both zero, or blank, your first Label35 = line will be executed and will overflow. Commented Aug 16, 2017 at 9:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Sounds good, but the OP's code is using Val which means all the calcs should be being done as Double. (At least a TypeName(Val("1")), and even TypeName(Val(CInt(1))), returns Double, so I can't see anything that would be dropping it back to an Integer.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.