VBA 子调用给出“编译错误:类型不匹配:需要数组或用户定义的类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19513697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA sub call gives "compile error: Type mismatch: array or user-defined type expected"
提问by user429400
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?
我有 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
Thanks, Li
谢谢,李
回答by sam092
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
你在这里传递一个长数组作为第二个参数