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

时间:2018-07-18 06:24:40

标签: 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

这段代码有效,但是我似乎无法实现的一件事是摆脱所有CSV文件的标题行 EXCEPT 第一个导入的CSV。

预期结果

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

1 个答案:

答案 0 :(得分:5)

您需要添加宏记录器没有自动神奇地包含的属性之一。 QueryTable.TextFileStartRow Property 允许您跳过文本文件顶部的许多行。

最好将其作为参数传递。

...
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