Ans
Ans

Reputation: 1234

Type mismatch: array or user-defined type expected, trying to pass array to a sub

I have an array and a displayArray sub which I try to use. However, I get a

Type mismatch: array or user-defined type expected error

Main sub:

Dim someArray() As Double
someArray = getBinsArray(sumLossesColl)
displayArray (someArray)

displayArray sub:

Sub displayArray(someArray() As Double)
    Dim i As Long


    With ThisWorkbook.Worksheets("Sheet1")
        .Range(.Cells(1, 1), .Cells(1, UBound(someArray) - LBound(someArray))).value = someArray
    End With
End Sub

'getBinsArray' function:

Function getBinsArray(dataArray() As Double)
    Dim binsNumber As Long
    Dim binSize As Double

    binsNumber = Round(VBA.Sqr(UBound(dataArray) - LBound(dataArray)) + 0.5)
    MsgBox ("binsNumber: " & binsNumber)

    binSize = (getMaxValue(dataArray) - getMinValue(dataArray)) / (binsNumber - 1)

    Dim resultArray() As Double
    ReDim resultArray(1 To bindsNumber) As Double
    resultArray(1) = getMinValue(dataArray)

    Dim i As Long
    For i = 2 To binsNumber
        resultArray(i) = resultArray(i - 1) + binSize
    Next


    getBinsArray = resultArray
End Function

What is the reason behind this? The types are Double, everything seems fine. How to fix it?

Upvotes: 0

Views: 502

Answers (1)

FunThomas
FunThomas

Reputation: 29171

Change to call to displayArray to one of the following two ways:

call displayArray (someArray)
displayArray someArray

So either use 'Call' and put the parameter(s) in brackets, or omit the brackets. There are endless discussions which version to use, but at the end it's a matter of taste.

Upvotes: 1

Related Questions