为什么函数返回零?

时间:2019-02-05 10:18:44

标签: excel vba

我有两个功能来读取csv数据文件并将其导入2D数组。使用简单的宏调用这些函数,我总是为导入的文件获得正确的数组大小,但是ImportCSV2Array()函数的返回值不一致。我在立即窗口中添加了诊断语句,这些语句表明分配为返回值(即数组的维)的函数变量在调用函数和被调用函数中都是正确的。但是,当我导入一些文件时,我得到Return 0,但我不明白为什么?请问有人可以建议我缺少什么吗?

这是调用函数

Public Function ImportDataLog() As Boolean

Dim headerArr() As Variant
Dim filePath As String
Dim csvret As Long

On Error Resume Next
' Open file dialog to get a test file name, including path.
filePath = Application.GetOpenFilename( _
                "Test Data Files (*.csv), *.csv", 1, _
                "Select Pressure Test File")
'Debug.Print "Selected file " & filePath

' Allocate basic array before using.
ReDim headerArr(1, 1)
csvret = 0
' Read csv file and fill array with data
Application.StatusBar = "Importing . . ."
csvret = ImportCSV2Array(filePath, headerArr)

' Here csvret and UBound(headerArr, 1) should match.
Debug.Print "Return " & csvret; " (UB) " & UBound(headerArr, 1) '!!

If csvret = 0 Then
    Application.StatusBar = "Error Importing Test Data!"
    ImportDataLog = False
    'Exit Function
End If

Debug.Print "LastRecord "; headerArr(UBound(headerArr, 1), UBound(headerArr, 2))
ImportDataLog = True
Application.StatusBar = False

End Function

csv文件通常是2行的“测试标头”文件或“测试结果”文件,具有以表格格式记录的数千条记录。当我导入“ Header”文件时,输出为:-

Allocated Array 1 Rows X 37 Columns
Return 1 (UB) 1
LastRecord 50.000
True

返回值和(UB)值符合预期。 但是“结果”文件的输出为:-

Allocated Array 8498 Rows X 9 Columns
Return 0 (UB) 8498
LastRecord 5
True

在这里,我不明白为什么会收到“ Return 0(UB)8498”?

我正在调用的导入函数是这样的:-

' Function for importing data from csv files to 2D array.
Public Function ImportCSV2Array(csvFileName As String, ByRef arr() As Variant) As Long

Dim row_number As Long
Dim col_Offset As Integer
Dim LineFromFile As String
Dim LineItems() As String
Dim LineNum As Long
Dim Field As Variant
Dim FileNumber As Integer

FileNumber = FreeFile
Close #FileNumber
row_number = 0
Application.Calculation = xlCalculationManual

' First pass to gauge the array dimensions required
Open csvFileName For Input As #FileNumber
Do Until EOF(FileNumber)
    Line Input #FileNumber, LineFromFile
    row_number = row_number + 1
Loop ' Until EOF(FileNumber)

If InStr(1, LineFromFile, "END") >= 1 Then
    row_number = row_number - 1
End If
LineItems = Split(LineFromFile, ",")
' ReDim tha array to fit the incoming data
ReDim arr(0 To row_number - 1, 0 To UBound(LineItems))
row_number = 0
' Diagnostic to check array sized OK
Debug.Print "Allocated Array " & UBound(arr, 1) & " Rows X " & UBound(arr, 2); " Columns"
Close #FileNumber

' Now import the data to the array line by line.
Open csvFileName For Input As #FileNumber
Do Until EOF(FileNumber)
    Line Input #FileNumber, LineFromFile
    LineItems = Split(LineFromFile, ",")

    For Each Field In LineItems
        If Field <> "" Then
            arr(row_number, col_Offset) = LineItems(col_Offset)
            col_Offset = col_Offset + 1
        End If
    Next Field
    row_number = row_number + 1
    col_Offset = 0
    LineNum = LineNum + 1
Loop ' Until EOF(FileNumber)
Close #FileNumber

' Set return value to show array's first dimension
ImportCSV2Array = UBound(arr, 1)

Application.Calculation = xlCalculationAutomatic

End Function

这两个函数似乎可以正常工作,即可以正常导入数据,但是我觉得如果返回值与预期不符,我会漏掉一个错误,该错误稍后在处理数组时可能很重要。有什么建议吗?

解决方案 捕获错误后,问题出在某些文件中,row_number的增量超过了UBound(arr, 1)。因此,通过一些错误检查,此解决方案有效:-

Do Until EOF(FileNumber)
    Line Input #FileNumber, LineFromFile
    LineItems = Split(LineFromFile, ",")
    If row_number > UBound(arr, 1) Or InStr(1, LineFromFile, "END") >= 1 Then
        Exit Do
    End If
    For Each Field In LineItems
        If Field <> "" Then
            arr(row_number, col_Offset) = LineItems(col_Offset)
            col_Offset = col_Offset + 1
        End If
    Next Field
    row_number = row_number + 1
    col_Offset = 0
Loop ' UnLineNumtil EOF(FileNumber)

给出预期的结果:

Return 8498 (UB) 8498
LastRecord 5
True

1 个答案:

答案 0 :(得分:1)

由于2,错误在这里:

Debug.Print "LastRecord "; headerArr(UBound(headerArr, 1), UBound(headerArr, 2))

因为这是数组的唯一修改位置,并且第二维中没有2

ReDim headerArr(1, 1)

VBA using ubound on a multidimensional array