我遇到了一些麻煩,搞清楚如何從另一個UDF返回UDF中的數組。這裏的一個是簡單的指數移動平均UDF,我試圖將數組返回到另一個UDF,但是我得到#value錯誤。我覺得有一個簡單的解決方案,我沒有看到。非常感謝所有幫助,謝謝。如何調用返回另一個UDF中的數組的UDF?
Function ema(arg1 As Variant, ByVal lngth As Long) As Variant
x = arg1
dim avg As Double
avg = 1
Dim arrema As Variant
arrema = Array()
ReDim arrema(1 To UBound(x, 1), 1 To 1)
For j = 1 To (UBound(x, 1) - lngth)
For i = (1 + j - 1) To (lngth + j - 1)
avg = (WorksheetFunction.Index(x, i, 1) + 1) * avg
Next i
arrema(j, 1) = avg^(1/lngth)
avg = 1
Next j
'ema = avg^(1/lngth)
ema = arrema
End Function
Function test(arg2 As Variant, xlength As Long)
Dim arra As Variant
'Call ema(arg2, xlength)
Dim arr As Variant
arr = Array()
ReDim arr(1 To UBound(arg2, 1), 1 To 1)
arra = ema(arg2, xlength)
For i = 1 To UBound(arg2, 1) - xlength
arr(i, 1) = arra(i, 1)
Next i
test = arr
End Function
測試函數的第一個參數需要arrary。 ** = test({1,2,3,4,5,9},10)** – Santosh 2013-04-08 00:09:40
不太確定我關注,我在arg1中傳遞與arg2中相同的數組。 – user2255731 2013-04-08 01:15:30
你傳遞了什麼值來獲得這個錯誤? – glh 2013-04-08 18:45:43