Overflow error when declaring variables

J

Jeff

Hi,

When I declare the variable I get an overflow error -
depending on how it is declared.

Public IFundTable()

then in
Sub Macro1()
Redim IFundTable(1 to 5, 1 to 158510) 'There is no overflow error
Redim IFundTable(1 to 5, 1 to Count*10) 'Count = 15851, there is an error

Why?

Thanks for your help
 
J

JE McGimpsey

How did you declare Count?

If as a Variant or Integer, then that's the cause.

When you do math on variables or constants, the compiler tries to fit
the result into the longest operand type. Since both 10 and 15851 fit
within the Integer type, the result is expected to be an Integer.
Unfortunately, the result doesn't fit within the Integer type so you get
the overflow.

Try:

Public Sub Macro1()
Dim Count As Long
Count = 15851
ReDim IFundTable(1 To 5, 1 To 158510) 'There is no overflow error
ReDim IFundTable(1 To 5, 1 To Count * 10)
End Sub

Alternatively, declare the 10 to be of type Long

ReDim IFundTable(1 To 5, 1 To Count * 10#)
 
J

Jeff

Yes that was the problem - I declared count as an integer - when it is a
double there is no problem.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top