简体   繁体   English

Excel VBA UDF-无法将UDF产生的变量数组作为第二个UDF中的输入传递

[英]Excel VBA UDF - Fail to pass a variant array resulting from UDFas an input in second UDF

I am struggling with using the array resulting from an UDF I have written as the argument to another UDF. 我正在努力使用我编写的UDF产生的数组作为另一个UDF的参数。

The function returns the #value error. 该函数返回#value错误。

I can not figure where the problem lies. 我不知道问题出在哪里。

Below is the code. 下面是代码。

Any help / advice would be greatly appreciated ;) 任何帮助/建议将不胜感激;)

This one works.. 这个作品..

Function fTA_GetSMA(ByRef varData As Variant, ByRef lPeriod As Long) As Variant

' This function computes a simple moving average over a defined period.

Dim l As Long
Dim dSum As Double
Dim var() As Variant

    Application.Volatile
    varData = varData.Value2
    ReDim var(LBound(varData, 1) To UBound(varData, 1), 1)
    For l = LBound(varData, 1) To UBound(varData, 1)
        If l < lPeriod Then
            dSum = dSum + varData(l, 1)
        ElseIf l = lPeriod Then
            dSum = dSum + varData(l, 1)
            var(l, 1) = dSum / lPeriod
        ElseIf l > lPeriod Then
            dSum = dSum + varData(l, 1) - varData(l - lPeriod, 1)
            var(l, 1) = dSum / lPeriod
        End If
    Next l
    fTA_GetSMA = var

End Function 结束功能

This one works also. 这个也可以。

Function fTA_GetTR(ByRef varData As Variant) As Variant ' This function computes the true range of a financial time series. 函数fTA_GetTR(ByRef varData As Variant)As Variant'此函数计算财务时间序列的真实范围。 ' The input data must be a matrix containing O, H, L, C. 输入的数据必须是包含O,H,L,C的矩阵。

Dim var() As Variant
Dim l As Long
Dim dMaxTR As Double
Dim dMinTR As Double

    Application.Volatile
    varData = varData.Value2
    ReDim var(LBound(varData, 1) To UBound(varData, 1), 1)
    For l = LBound(varData, 1) To UBound(varData, 1)
        If l = 1 Then
            dMinTR = varData(l, 3)
            dMaxTR = varData(l, 2)
        ElseIf l > 1 Then
            dMaxTR = Application.WorksheetFunction.Max(varData(l, 2), varData(l - 1, 4))
            dMinTR = Application.WorksheetFunction.Min(varData(l, 3), varData(l - 1, 4))
        End If
        var(l, 1) = dMaxTR - dMinTR
    Next l

    fTA_GetTR = var

End Function 结束功能

THIS IS THE ONE which is not working.. 这是一个不起作用的..

Function fTA_GetATR(ByRef varData As Variant, ByRef lPeriod As Long) As Variant ' This function computes the average true range of a financial time series over a given number of periods. 函数fTA_GetATR(ByRef varData为变体,ByRef lPeriod为长)作为变体'此函数计算给定期间内金融时间序列的平均真实范围。 ' the input data must be a matrix containing O, H, L, C. ' the average formula is a SMA “输入数据必须是包含O,H,L,C的矩阵。”平均公式是SMA

Dim var() As Variant
Dim varATR() As Variant

    ReDim var(LBound(varData, 1) To UBound(varData, 1), 1)
    ReDim varATR(LBound(varData, 1) To UBound(varData, 1), 1)
    var = fTA_GetTR(varData)
    varATR = fTA_GetSMA(var, lPeriod)
    Debug.Print varATR
    fTA_GetATR = varATR

End Function 结束功能

You can only use VarData.Value2 if if Var contains a range reference: it won't work if VarData is a variant array. 如果Var包含范围引用,则只能使用VarData.Value2:如果VarData是变量数组,则它将不起作用。
Your UDFs expect Range arguments as input (because you use .Value2) but return a variant containing an array. 您的UDF希望将Range参数作为输入(因为您使用.Value2),但是返回包含数组的变量。 So when you call one of them using the output from a different UDF it fails because the input is not a range. 因此,当您使用其他UDF的输出调用其中之一时,由于输入不是范围,因此它会失败。
You can detect this by setting a breakpoint and showing the Locals window to inspect what the Var variable contains. 您可以通过设置一个断点并显示Locals窗口来检查Var变量包含的内容来检测到这一点。
The way to handle this is to do something like this 解决这个问题的方法是做这样的事情

if IsObject(VarData) then VarData=VarData.Value2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM