首次导入后删除CSV导入的标题行

Flo*_*low 2 excel vba excel-vba

我一直致力于获取一段代码,该代码正在工作并从给定目录导入所有CSV文件.

Sub ImportAllCsv()
  Dim FName As Variant, R As Long

  Application.DisplayAlerts = False
    On Error Resume Next
  ThisWorkbook.Sheets("CSV data").Delete
    On Error GoTo 0
  Application.DisplayAlerts = True
  ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count)).Name = "CSV data"

  R = 1
  FName = Dir("C:\VBA\CSVs\*.csv")
  Do While FName <> ""
    ImportCsvFile FName, Sheets("CSV data").Cells(R, 1)
    R = Sheets("CSV data").UsedRange.Rows.Count + 1
    FName = Dir

  Loop
End Sub

Sub ImportCsvFile(FileName As Variant, Position As Range)
  With Sheets("CSV data").QueryTables.Add(Connection:= _
      "TEXT;" & "C:\VBA\CSVs\" & FileName, Destination:=Position)
      .FieldNames = True
      .RowNumbers = False
      .FillAdjacentFormulas = False
      .RefreshOnFileOpen = False
      .BackgroundQuery = True
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = False
      .SaveData = True
      .AdjustColumnWidth = True
      .TextFilePromptOnRefresh = False
      .TextFilePlatform = xlMacintosh
      .TextFileStartRow = 1
      .TextFileParseType = xlDelimited
      .TextFileTextQualifier = xlTextQualifierDoubleQuote
      .TextFileConsecutiveDelimiter = False
      .TextFileTabDelimiter = True
      .TextFileSemicolonDelimiter = False
      .TextFileCommaDelimiter = False
      .TextFileSpaceDelimiter = False
      .TextFileOtherDelimiter = ","
      .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
      .Refresh BackgroundQuery:=False
  End With
End Sub
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但是我似乎无法实现的一件事是除了第一个导入的CSV之外,除去所有CSV文件的标题行.

预期结果

获取代码以排除在第一个文件之后导入的所有CSV文件的标题行.

小智 5

您需要添加宏录制器不会自动包含的其中一个属性.该QueryTable.TextFileStartRow属性允许你在文本文件的顶部跳过的行数.

这可能最好作为参数传递.

...
R = 1
FName = Dir("C:\VBA\CSVs\*.csv")
Do While FName <> ""
    ImportCsvFile FName, Sheets("CSV data").Cells(R, 1), abs(r<>1)+1
    R = Sheets("CSV data").UsedRange.Rows.Count + 1
    FName = Dir
Loop

Sub ImportCsvFile(FileName As Variant, Position As Range, startRow as long)
  With Sheets("CSV data").QueryTables.Add(Connection:= _
      "TEXT;" & "C:\VBA\CSVs\" & FileName, Destination:=Position)
      .TextFileStartRow = startRow   '<~~ new parameter
      .FieldNames = True
      .RowNumbers = False
      .FillAdjacentFormulas = False
      .RefreshOnFileOpen = False
      .BackgroundQuery = True
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = False
      .SaveData = True
      .AdjustColumnWidth = True
      .TextFilePromptOnRefresh = False
      .TextFilePlatform = xlMacintosh
      .TextFileStartRow = 1
      .TextFileParseType = xlDelimited
      .TextFileTextQualifier = xlTextQualifierDoubleQuote
      .TextFileConsecutiveDelimiter = False
      .TextFileTabDelimiter = True
      .TextFileSemicolonDelimiter = False
      .TextFileCommaDelimiter = False
      .TextFileSpaceDelimiter = False
      .TextFileOtherDelimiter = ","
      .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
      .Refresh BackgroundQuery:=False
  End With
End Sub
Run Code Online (Sandbox Code Playgroud)