我在使用VBA函数时遇到了一些问题,它适用于小于5000的值,但是当我提供大量数据时,例如10.000,函数只会在excel中返回错误。你知道为什么吗?这是代码:
Function VWAP(Prices As Variant, Volume As Variant) As Variant
Dim i As Long
Dim n As Long
n = Prices.Rows.Count
ReDim Z(n, 1)
Dim result As Double
Dim Lots As Integer
Lots = 0
result = 0
For i = 1 To n
Z(i, 1) = (Prices(i, 1) * Volume(i, 1))
Lots = Lots + Volume(i, 1)
result = result + Z(i, 1)
Next i
VWAP = result / Lots
End Function
答案 0 :(得分:1)
这是因为你对Lots使用了一个整数
只需Dim Lots as Long
,它就不会溢出