Overflow in VBA, but value < limit

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

Okay, I¹ve declared a value ³total² as Long ...

The following causes an overflow ... Of the long integer ³total²

Does not ... Is there a limit to the number of multiply signs I can include
in a line??

Obviously, I¹m not doing this actual sum in my macro multiplies together a
number of variables (a, b, c, e ... Etc)

How can I overcome this limitation?

Cheers,

Brad.)
 
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)

--------------------
 
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
 
Back
Top