Overflow in VBA, but value < limit
Just to add. If Excel sees that just one of the values is long, it will use
that. Here, only the first 2 is "2&."
Sub Demo()
Dim Total As Long
Total = 2& * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
End Sub
--
Dana DeLouis
= = = = = = = = = = = = = = = = =
"Colo" wrote in message
...
Hi Brad,
The NUMBER 2 is considered to be an INTEGER type in Excel.
See Debug.Print TypeName(2). It returns "Integer".
A variable "total" has been declared as long but Excel need to
calculate before input value to "total".
It return a message "Overflow" when the value is over 32,767.(The limit
of Integer type)
So change the code something like below ..
Code:
--------------------
total = 2& * 2& * 2& * 2& * 2& * 2& * 2& * 2& * 2& * 2& * 2& * 2& * 2& *
2& * 2& * 2&
--------------------
Or
Code:
--------------------
total = CLng(2) * CLng(2) * CLng(2) * CLng(2) * CLng(2) * _
CLng(2) * CLng(2) * CLng(2) * CLng(2) * CLng(2) * _
CLng(2) * CLng(2) * CLng(2) * CLng(2) * CLng(2) * CLng(2)
--------------------
|