Overflow error.. why?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dim x As Long
x = 2000 * 365

Why is this statment generate a : Overflow (Error 6)
I dont understant it since x is "long" and should be able to store this
large number.

Could someone please try to explain it.

I know that the statement under will solve it.
Dim x As Long
x = CLng(2000) * 365

Best regards
Petter Bøhler
 
When doing its intermediate calculations, VBA uses the smallest data type
that it can to hold the variables. Since both 2000 and 365 can be stored in
Integers, VBA uses integers in its internal calculations. But 2000 * 365 =
730000 exceeds the maximum value of an integer, thus you get the "Overflow"
error. You can force VBA to use Longs by suffixing one of the operands with
the Long type definition character '&'. E.g.,

Dim x As Long
x = 2000& * 365


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)
 
Chip: Is 2000& better than CLng (2000)? Is the former done at compile
time, and therefore not calling to a function?

Dom
 
Yes, 2000& is better than CLng(2000) for the reason you suspect. 2000& is
made a Long at compile time, and CLng is a function call done at run time.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)

Chip: Is 2000& better than CLng (2000)? Is the former done at compile
time, and therefore not calling to a function?

Dom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Access Overflow error 3
Userform on two monitors 0
Overflow Error 2
Overflow 4
coding error I don't know how to debug? 1
Overflow error 6 3
How to delete multiple files? 7
Macro to add Multiple columns 5

Back
Top