gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:42:42 26 4
gpt4 key购买 nike

我一直在努力获取一段代码,该代码正在运行并从给定目录导入所有 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 文件的标题行除了第一个导入的 CSV。

预期结果

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

最佳答案

您需要添加宏录制器不会自动包含的属性之一。 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

关于vba - 首次导入后删除 CSV 导入的标题行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51394908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com