Overflow Error But Right Answer is Produced
Try this line. You are trying to round a range of cells. You need to sum
them first or calculate them to get a single value.
NumShares = Round(WorksheetFunction.Sum(aNumShrs(lRow, 1)), 2)
Hope this helps! If so, let me know, click "YES" below.
--
Cheers,
Ryan
"MichaelDavid" wrote:
Hi Ryan
I forgot to mention that array aNumShares is numeric. It contains mostly
Long variables but occasionally contains a few Double Precision Variables.
--
May you have a most blessed day!
Sincerely,
Michael Fitzpatrick
"Ryan H" wrote:
You need to ensure that aNumShrs(21,1) is a numeric expression. What is
aNumShrs(lRow, 1)? Is it a Sub? Is it a function that returns a Double data
type? Maybe try testing if aNumShrs(lRow, 1) is numeric first. Hope this
helps! If so, let me know, click "YES" below.
Sub UserCode()
If IsNumeric(aNumShrs(lRow, 1)) Then
NumShares = Round(aNumShrs(lRow, 1), 2)
NumSharesLong = NumShares
Else
MsgBox "aNumShrs is not numeric."
End If
End Sub
--
Cheers,
Ryan
"MichaelDavid" wrote:
Greetings! The below code generates an overflow error when aNumShrs(21, 1)
contains 15749.9999602351, and the macro executes the line which reads:
TotalSharesPurchased = TotalSharesPurchased + aNumShrs(lRow, 1)
Dim aNumShrs As Variant
Dim aPrice As Variant
Dim TotalPurchases As Double
Dim TotalSharesPurchased As Long
If aPrice(lRow, 1) - MaxPrice 0# Then MaxPrice = aPrice(lRow, 1)
TotalPurchases = TotalPurchases + aPrice(lRow, 1) * aNumShrs(lRow, 1)
TotalSharesPurchased = TotalSharesPurchased + aNumShrs(lRow, 1)
When I modified the above code as follows:
Dim aNumShrs As Variant
Dim aPrice As Variant
Dim TotalPurchases As Double
Dim TotalSharesPurchased As Long
Dim NumShares As Double
Dim NumSharesLong As Long
NumShares = Round(aNumShrs(lRow, 1), 2)
NumSharesLong = NumShares
If aPrice(lRow, 1) - MaxPrice 0# Then MaxPrice = aPrice(lRow, 1)
TotalPurchases = TotalPurchases + aPrice(lRow, 1) * aNumShrs(lRow, 1)
TotalSharesPurchased = TotalSharesPurchased + NumSharesLong
I still get the overflow error, but I can tell by holding my cursor over the
variables in the above, that the right answer is being generated. But the
overflow error prohibits the program from continuing. So I then added On
Error Resume Next to the above logic as follows:
On Error Resume Next
NumShares = Round(aNumShrs(lRow, 1), 2)
NumSharesLong = NumShares
On Error GoTo 0
If aPrice(lRow, 1) - MaxPrice 0# Then MaxPrice = aPrice(lRow, 1)
TotalPurchases = TotalPurchases + aPrice(lRow, 1) * aNumShrs(lRow, 1)
TotalSharesPurchased = TotalSharesPurchased + NumSharesLong
Now the logic runs without a hitch and produces the right answer, and the
macro containing the above logic runs to completion. Is there any way to code
this without using On Error Resume Next?
--
May you have a most blessed day!
Sincerely,
Michael Fitzpatrick
|