Excel VBA Ubound / Lbound错误

时间:2018-07-30 19:27:16

标签: excel vba excel-vba

UBound似乎没有返回任何东西。我在Excel中使用Pricer函数,并将其传递给列。我的数据类型不匹配吗?我在访问arrP.Value的虚拟数组上绑定了对象,但也没有起作用。有想法吗?

Function Pricer(arrP As Variant) As Double
        sd = Application.WorksheetFunction.StDevP(arrP)
        avg = Application.WorksheetFunction.Average(arrP)
        PriceUB = avg + sd
        PriceLB = avg - sd
        MsgBox UBound(aarP)
        Pricer = Application.WorksheetFunction.Average(arrP)
End Function

1 个答案:

答案 0 :(得分:0)

Option Explicit确实可以在您下一次遭受“胖手指”痛苦时为您节省。只是不要忘记声明其他变量:

Option Explicit

Public Sub TestMe()    
    MsgBox Pricer(Array(1, 2, 3, 4, 5, 100))    
End Sub

Function Pricer(arrP As Variant) As Double

    Dim sd, avg, PriceUB, PriceLB
    sd = Application.WorksheetFunction.StDevP(arrP)
    avg = Application.WorksheetFunction.Average(arrP)
    PriceUB = avg + sd
    PriceLB = avg - sd
    MsgBox UBound(arrP)
    Pricer = Application.WorksheetFunction.Average(arrP)

End Function

要在Excel工作表中使用公式,请像这样传递它:

enter image description here

分号可能是逗号,具体取决于您的语言设置:

enter image description here


要使Pricer()Range()一起用作Excel公式,请执行以下操作:

Function Pricer(arrP As Range) As Double

    MsgBox arrP.Cells.Count
    Pricer = Application.WorksheetFunction.Average(arrP)

End Function