将CSV合并到一个Excel工作表并删除标题

我试图将存储在一个文件夹中的所有CVS's合并到一个Excel工作表中。 合并之后,我将运行一个单独的macros来处理所有格式,而不是单独格式化每个单独的CSV文件。

下面的代码是我到目前为止:

 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行。

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

我基本上只是希望文件看起来像一个,而不是看起来像文件复制和粘贴到工作表。

任何帮助,将不胜感激。

对现有代码的最简单的修改就是只包含代码,如果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