ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Overflow in VBA, but value < limit (https://www.excelbanter.com/excel-programming/285093-overflow-vba-but-value-limit.html)

Brad[_13_]

Overflow in VBA, but value < limit
 
Okay, Iıve declared a value ³total² as Long ...

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

total = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2


But

Total = 2 ^ 16


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


Colo[_12_]

Overflow in VBA, but value < limit
 
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)

--------------------



---
Message posted from http://www.ExcelForum.com/


Dana DeLouis[_3_]

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)

--------------------




Colo[_15_]

Overflow in VBA, but value < limit
 
Dana, thanks for your input. :D


---
Message posted from http://www.ExcelForum.com/



All times are GMT +1. The time now is 04:53 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com