VBA: 将多个 csv 文件导入现有 table

VBA: import multiple csv file into an existing table

我想在现有 table 的底部导入多个 csv 文件。但是,在导入文件时,它总是排除每个文件列表的第一行。列表的第一行与电子表格的第一行不同,因为中间有其他不需要的行(例如标题、空行...)。正在恢复:如果我上传 5 个文件,它会错过这 5 个文件中每个文件的第一行。

这是代码:

Private Sub Import_auction_offers_Click()
    Dim strSourcePath As String
    Dim strFile As String
    Dim Cnt As Long

    'Change the path to the source folder accordingly
    strSourcePath = "C:\Users\L18944\Desktop\example"

    If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"
    strFile = Dir(strSourcePath & "*.csv")

    Do While Len(strFile) > 0
        Cnt = Cnt + 1

Open strSourcePath & strFile For Input As #1

If Range("F2").Value <> "" Then
    Range("F1").End(xlDown).offset(1, 0).Select
Else:
    Range("F1:F" & Range("F" & Rows.Count).End(xlUp).Row).offset(1, 0).Select
End If
currentRow = 0
rowNumber = 0

'EOF(1) checks for the end of a file
Do Until EOF(1)
    Line Input #1, lineFromFile
    fileStr = Split(lineFromFile, vbLf)
    Dim item As Variant
    For Each item In fileStr
    'For item = LBound(fileStr) To UBound(fileStr)
        lineitems = Split(item, ";")
        'Debug.Print (item)
        If rowNumber = 1 Then
            startDate = lineitems(6)
        End If
        If rowNumber > 3 And item <> "" Then
            If Not doesOfferExist(CStr(lineitems(2))) Then
                ActiveCell.offset(currentRow, 0) = startDate
                ActiveCell.offset(currentRow, 1) = lineitems(4)
                ActiveCell.offset(currentRow, 2) = lineitems(3)
                ActiveCell.offset(currentRow, 3) = CDbl(lineitems(6))
                ActiveCell.offset(currentRow, 4) = CDbl(lineitems(7))
                ActiveCell.offset(currentRow, 5) = lineitems(8)
                ActiveCell.offset(currentRow, 6) = lineitems(1)
                ActiveCell.offset(currentRow, 7) = lineitems(2)
                ActiveCell.offset(currentRow, 8) = "New"
                currentRow = currentRow + 1
            End If
        End If

        rowNumber = rowNumber + 1
    Next item
Loop
Close #1
 Name strSourcePath & strFile As strSourcePath & strFile
        strFile = Dir
    Loop

  Application.ScreenUpdating = True

    If Cnt = 0 Then _
        MsgBox "No CSV files were found...", vbExclamation

End Sub

有谁知道为什么它会漏掉每个导入列表的第一行? 提前谢谢你

我没有查看您的 ImportAuctionOffers 代码,但我假设您正在为每个文件找到新的起始行。

此代码将让您选择文件(并设置初始目录)。然后遍历所有选定的项目,为每个文件调用您的 ImportAuctionOffers 过程。

Sub test()
    Dim oFileDialog As FileDialog

    Set oFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    oFileDialog.AllowMultiSelect = True
    oFileDialog.InitialFileName = "C:\Temp"    ' can set your default directory here

    oFileDialog.Show

    Dim iCount As Integer
    For iCount = 1 To oFileDialog.SelectedItems.Count
        Call ImportAuctionOffers(oFileDialog.SelectedItems(iCount))
    Next

End Sub

更新:

对于您的第二个问题:未读取第一行数据可能是由于带有 RowNumber 的 if 语句。

rowNumber=0

Do ...
    if RowNumber = 1 Then ...
    if RowNumber > 3 ...

    RowNumber = RowNumber + 1
loop

当 RowNumber 等于 0、2 或 3 时,您的代码不会进入任何 if 语句。您可能只需要将 > 3 更改为 > 2 或 >= 3。