ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Overflow error when declaring variables (https://www.excelbanter.com/excel-discussion-misc-queries/172258-overflow-error-when-declaring-variables.html)

Jeff

Overflow error when declaring variables
 
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


JE McGimpsey

Overflow error when declaring variables
 
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#)

In article ,
Jeff wrote:

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


Jeff

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

Thanks

"JE McGimpsey" wrote:

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

In article ,
Jeff wrote:

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




All times are GMT +1. The time now is 03:43 AM.

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