简体   繁体   中英

VBA sub call gives “compile error: Type mismatch: array or user-defined type expected”

I have 2 Subs, both receive array as argument. one works fine, the other gives: compile error: Type mismatch: array or user-defined type expected. In the code written bellow, "InitializeArray" works and "PresentTotalRow" does not work. Can anyone figure out why?

Sub PresentTotalRow(nCells As Integer, totalProductsPerDay() As Integer)
    row = nCells + MatrixRowOffset + 2
    Range(Cells(row, 2), Cells(row, 8)) = totalProductsPerDay
End Sub

Sub InitializeArray(ByRef arr() As Long)
    Dim N As Long
    For N = LBound(arr) To UBound(arr)
        arr(N) = 0
    Next N
End Sub


Sub ReadTxtFile()
    .....

    Dim totalProductsPerDay(0 To 6) As Long
    InitializeArray totalProductsPerDay

    Dim filePath As String
    filePath = "C:\work\Documents\input.txt"

    Dim oFS As TextStream
    If oFSO.FileExists(filePath) Then

        Set oFS = oFSO.OpenTextFile(filePath)
        ......
        i = 1
        Do While Not oFS.AtEndOfStream

            line = oFS.ReadLine
            ....
            nCells = calcNCells                

            totalProductsCounter = GetTotalProductsCounter()
            totalProductsPerDay(Day) = totalProductsPerDay(Day) + totalProductsCounter

            i = i + 1
        Loop

        PresentTotalRow nCells, totalProductsPerDay
        oFS.Close
    Else
        MsgBox "The file path is invalid.", vbCritical, vbNullString
    Exit Sub
End If

Exit Sub

End Sub

Thanks, Li

Sub PresentTotalRow(nCells As Integer, totalProductsPerDay() As Integer)
    row = nCells + MatrixRowOffset + 2
    Range(Cells(row, 2), Cells(row, 8)) = totalProductsPerDay
End Sub

the second argument expects an integer array

PresentTotalRow nCells, totalProductsPerDay

you are passing an long array here as the second argument

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