VBA子调用给出“编译错误:类型不匹配:数组或用户定义的类型预期”

时间:2013-10-22 09:15:33

标签: arrays excel-vba argument-passing vba excel

我有2个Subs,都接收数组作为参数。一个工作正常,另一个工作正常:编译错误:类型不匹配:数组或用户定义的类型。 在下面编写的代码中,“InitializeArray”工作,“PresentTotalRow”不起作用。 任何人都可以找出原因吗?

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

谢谢, 李

1 个答案:

答案 0 :(得分:2)

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

第二个参数需要一个整数数组

PresentTotalRow nCells, totalProductsPerDay

你在这里传递一个长数组作为第二个参数