【问题标题】:Merging CSVs into one sheet and removing headers将 CSV 合并到一张表中并删除标题
【发布时间】:2017-09-09 22:33:46
【问题描述】:

我将一个文件夹中的所有 CSV 文件合并到一个 Excel 工作表中。

Sub MergeFiles_Click()

    Dim strSourcePath As String
    Dim strDestPath As String
    Dim strFile As String
    Dim strData As String
    Dim x As Variant
    Dim Cnt As Long
    Dim r As Long
    Dim c As Long
    
    Application.ScreenUpdating = False
    
    strSourcePath = Sheet1.Range("G2").Value
    
    If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"
    
    strFile = Dir(strSourcePath & "*.csv")
    
    Do While Len(strFile) > 0
        
        Cnt = Cnt + 1
        
        If Cnt = 1 Then
            r = 6
        Else
            r = Cells(Rows.Count, "A").End(xlUp).Row + 1
        End If
        
        Open strSourcePath & strFile For Input As #1
        Do Until EOF(1)
            Line Input #1, strData
            x = Split(strData, ",")
            For c = 0 To UBound(x)
                Cells(r, c + 1).Value = Trim(x(c))
            Next c
            r = r + 1
        Loop
            
        Close #1

        strFile = Dir
    Loop
    
    Application.ScreenUpdating = True
    
    If Cnt = 0 Then _
        MsgBox "No CSV files were found...", vbExclamation

End Sub

这会将所有 CSV 文件合并到一张纸上,但每个 CSV 文件在顶部都有一个标题和其他信息,占 12 行。

我想为第一个 CSV 保留 12 行,但在放入 Excel 工作表之前将它们从其余文件中删除。

我希望文件显示为一个,而不是看起来像是文件被复制并粘贴到工作表上。

【问题讨论】:

  • 您知道您正在使用的数据,但请注意 CSV 格式通常允许将逗号嵌入文字字符串(用双引号括起来的字段“like this”)。如果你得到其中任何一个,你的代码就会失败。
  • @RichHolton 所以在测试后我发现了一些导致问题的实例。我该怎么做才能避免这个问题?
  • 您可能会发现这个问题/答案很有帮助:stackoverflow.com/questions/12197274/…
  • @RichHolton 我浏览了那篇文章并抓取了使用 QueryTables 导入 csv 的代码并对其进行了测试,导入一个文件可以正常工作。我该如何重新安排它以继续并抓取文件夹中的每个 csv 以实现我想要做的事情?

标签: vba excel csv


【解决方案1】:

对现有代码的最简单更改是仅包含代码以仅在 Cnt 为 1 时复制前 12 行,否则忽略它们:

Sub MergeFiles_Click()

    Dim strSourcePath As String
    Dim strDestPath As String
    Dim strFile As String
    Dim strData As String
    Dim x As Variant
    Dim Cnt As Long
    Dim r As Long
    Dim c As Long
    Dim inputRow As Long

    Application.ScreenUpdating = False

    strSourcePath = Sheet1.Range("G2").Value

    If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"

    strFile = Dir(strSourcePath & "*.csv")

    Do While Len(strFile) > 0

        Cnt = Cnt + 1

        If Cnt = 1 Then
            r = 6
        Else
            r = Cells(Rows.Count, "A").End(xlUp).Row + 1
        End If


        Open strSourcePath & strFile For Input As #1
        inputRow = 0
        Do Until EOF(1)
            Line Input #1, strData
            'Maintain a count of how many rows have been read
            inputRow = inputRow + 1
            'Only process rows if this is the first file, or if we have
            'already passed the 12th row
            If Cnt = 1 Or inputRow > 12 Then
                x = Split(strData, ",")
                For c = 0 To UBound(x)
                    Cells(r, c + 1).Value = Trim(x(c))
                Next c
                r = r + 1
            End If
        Loop

        Close #1

        strFile = Dir
    Loop

    Application.ScreenUpdating = True

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

End Sub

【讨论】:

    【解决方案2】:

    正如 Yow E3K 所说,您第一次可以只复制前十二行。 我的偏好是先将它们放在模板上,然后再从不复制它们。

    下面的代码(来自 VBA Copy data from an unopened CSV file to worksheet without opening closed CSV - 谢谢 Chancea)已在中途进行了修改,以便通过输入从第 2 行开始复制 .TextFileStartRow = 2

    Sub ImportFromCSVWithoutHeaders()
    
    Dim MyDocuments, strFileName, myToday, file, strConnection As String
    
    MyDocuments = Environ$("USERPROFILE") & "\My Documents"
    myToday = Format(Date, "mmddyy")
    strFileName = "DataFile" & myToday & ".csv"
    
    Dim row As Integer
    row = 1
    On Error Resume Next
    row = Range("A1048576").End(xlUp).row + 1
    
    strConnection = "TEXT;" & MyDocuments & "\DataFolder\" & strFileName
    
    With ActiveSheet.QueryTables.Add(Connection:= _
         strConnection, Destination:=Range("$A$" & row))
        .Name = "temp"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        '.TextFileStartRow = 1
        .TextFileStartRow = 2
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    End Sub
    

    【讨论】:

    • 为什么第2行,OP提到12行重复数据?您需要区分第一个文件和其他文件,还建议删除.Refresh之后的QueryTable,否则工作簿可能会变得太大。
    猜你喜欢
    • 2021-05-13
    • 2014-10-15
    • 1970-01-01
    • 2013-07-13
    • 2019-02-28
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多