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

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

Ans Published in 2018-01-12 12:44:14Z

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

    getBinsArray = resultArray
End Function

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

FunThomas Reply to 2018-01-12 12:57:43Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.35614 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO