65

I just created a simple sub and it gives an overflow error. However, I don't see anything wrong with the code, and it is really weird since 50000*100 is much bigger than 500*100.

sub add()
    'This will cause an overflow error
    cells(1,1) = 500 * 100
    'But this won't
    cells(2,2) = 50000 * 100
end sub
4
  • 1
    Interestingly it does NOT cause the overflow error if you specify 50000 instead of 500 * 100...
    – KFichter
    Aug 4, 2015 at 18:19
  • 6
    Hint: Try CLng(500)*100 Aug 4, 2015 at 18:23
  • 4
    Yeah I guess 500 and 100 are being handled as integers so the product is also being handled as an integer, and 50000 is over the 32767 limit so here we are.
    – KFichter
    Aug 4, 2015 at 18:24
  • @KFichter 16 bit signed type?
    – Alec Teal
    Aug 5, 2015 at 1:38

4 Answers 4

74

Consider:

Sub add()
    'This works:
    Cells(1, 1) = CLng(500) * 100
    'as does this:
    Cells(2, 2) = 50000 * 100
End Sub

Evidently VBA was picking a default type of Integer for the first expression because that type is large enough to hold the literals on the right hand side. 50000 is too big for an Integer so it interprets it as a Long. CLng explicitly triggers a promotion to Long.

3
  • 2
    Or shorthand Cells(1, 1) = 500& * 100&
    – AndrewD
    Aug 12, 2015 at 0:44
  • @AndrewD It's enough to add the ampersand to just ONE of the numbers: 500 * 100&
    – Excel Hero
    Apr 2, 2020 at 6:20
  • @ExcelHero true, it will do implicit conversion - but I prefer to be explicit to be safe...if I'm reviewing code I consider an implicit to be a "smell" because sometimes people get the assumed result type wrong (I work in a number of languages and have got this wrong on a late night :( )
    – AndrewD
    Apr 4, 2020 at 2:33
20

The maximim value for a Integer is 32767 and since 50000 is more it is cast as a Long type. Thus the results fits in Long just fine. But in the first case everything is done with Integer types and it overflows.

(Integer=500) * (Integer=100) = (Integer=50000)  'Overflow
(Long=50000) * (Integer=100) = (Long=5000000)    'Ok
9

This is because of how VBA evaluates mathematical expressions. The return type of expression will be the type of first operand in the expression or its nearest numeric type equivalent. However, the final result may not fit in that return type and throw overflow error.

When you do 500*100 , return type is integer. While when you do 50000*100 the return type of this expression is Long.

To avoid overflow error you can do an explicit cast to let it know your intentions

CLng(500) * 100

2

i got the answer from the following link: Link from microsoft

it seems that even I did not assign a type to the number, excel automatically assign one to it based on its length. Thus, 500 is defined as integer and the result 50,000 is too big for type integer. That's why.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.