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
Community
  • 1
  • 1
  • 1
    Sounds like you're trying to divide by zero. Check the values of Label9 and Label27 are not zero. – Olly Aug 16 '17 at 08:26
  • No, division by zero raises runtime error 11 – FunThomas Aug 16 '17 at 08:40
  • Which line do you get the error on? What are the values of your `Label27`, etc, variables when it crashes? – YowE3K Aug 16 '17 at 08:47
  • when i run combobox in userform,the label 27 have not value yet,i think it be equal zero as default. – Sasan Fadakar Aug 16 '17 at 08:57
  • When it crashes, hover your mouse over each `Label27` type variable in the code - it should show what the actual value is. That's what we need to know, not just what you **think** it will be. (If our programs always used the values we **thought** they should be using, they would never crash on us! :D ) – YowE3K Aug 16 '17 at 08:59
  • 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. – YowE3K Aug 16 '17 at 09:01
  • @YowE3K; You nailed it. Another quirk of VBA... You should post this as answer so that we can upvote and make this the solution. – FunThomas Aug 16 '17 at 09:03
  • label27 and label 9 is zero.thanks @Olly – Sasan Fadakar Aug 16 '17 at 09:06
  • thanks for guidance @yowe3k – Sasan Fadakar Aug 16 '17 at 09:08
  • @FunThomas - welcome to the club - https://stackoverflow.com/questions/45485285/why-is-0-divided-by-0-throwing-an-overflow-error-in-vba – Vityata Aug 16 '17 at 09:21
  • @FunThomas Please check out Olly's answer and upvote if you feel it is appropriate. – YowE3K Aug 16 '17 at 15:31

2 Answers2

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.

YowE3K
  • 23,852
  • 7
  • 26
  • 40
Olly
  • 7,749
  • 1
  • 19
  • 38
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

FunThomas
  • 23,043
  • 3
  • 18
  • 34
  • 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`.) – YowE3K Aug 16 '17 at 08:46