简体   繁体   中英

VBA - Excel custom defined function gives me datatype error

This is my first time coding in VBA. Can anyone help me? It gives me unvalid datatype input. Where is the bug? Thank you.

Public Function prum(Data1 As Range) As Double()
    Dim X
    Dim result(1) As Double
    Dim count As Integer
    Dim i As Integer
    X = Data1
    count = UBound(X)

    result(0) = 0
    result(1) = 0

    For i = 1 To UBound(X)
        result(0) = X(i) + result(0)
    Next i
    result(0) = result(0) / count


    For i = 1 To UBound(X)
        result(1) = (X(i) - result(0)) * (X(i) - result(0)) + result(1)
    Next i

    result(1) = Sqr(result(1) / (count - 1) / count)


    prum = result
End Function

The problem is that X is a 2 dimensional array. You can convert the range values to a 1D array using Application.Transpose .

X = Application.Transpose(Data1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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