View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rowan Drummond[_3_] Rowan Drummond[_3_] is offline
external usenet poster
 
Posts: 414
Default A curious OVERFLOW problem

It has to do with the way vba does the calculation. Because 182 is an
integer the result which is stored temporarily before being assigned to
the variable x is expected to be an integer. You can get around this by
specifying 182 as a long first either with:

Sub test()
Dim x As Long
Dim num As Long
num = 182
x = num * num
MsgBox x

End Sub

or

Sub test()
Dim x as Long
x= clng(182)*clng(182) 'overflow error on this line
Msgbox x
End sub

See a similar query he http://tinyurl.com/ctlgv

Hope this helps
Rowan

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