View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Jay Petrulis[_2_] Jay Petrulis[_2_] is offline
external usenet poster
 
Posts: 13
Default A curious OVERFLOW problem

Don't know the details of exactly why this occurs, but when you get the
error, choose Help from the popup. The last part indicates that the
numbers are being coerced into an Integer, not Long (Integers). Thus,
182 is seen as an integer and the multiplication overflows the limit
for (signed) integers. The Long declaration is of no consequence.
VB[A] sees (short) ints only.

All of the following work, so you might want to restructure your
calculations. My preference would be for test3, but a workaround is
definitely needed.

'''''''''''''''''''''''''''''''''''''''
Sub test()
Dim x As Long

x = CLng(182) * 182
MsgBox x
End Sub

Sub test2()
Dim x As Long

x = 182
MsgBox x * x
End Sub

Sub test3()
Dim x As Long
Const kVal As Long = 182

x = kVal * kVal
MsgBox x
End Sub

Sub test4()
Dim x As Long

x = 182 ^ 2
MsgBox x
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Myles wrote:
Has anyone attempted to perform 182*182 operation in VBA? The following
code generates an ovwerflow error in spite of the declarations.

Sub test()
Dim x as Long

x= 182*182 'overflow error on this line
Msgbox x

End sub

The problem persists even with a DOUBLE declaration for x. Treating x
as a variant either by default (no declaration) or explicitly (by
declaration) doesn't help either.

As 181*181=32761, which reminds one of the magical figure 2 ^15 there
must be some connection here.


What's happening ?


Myles


--
Myles
------------------------------------------------------------------------
Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=492084