Vba 将CSV合并到一张图纸中并删除标题
我将文件夹中的所有CSV文件合并到一个Excel工作表中Vba 将CSV合并到一张图纸中并删除标题,vba,excel,csv,Vba,Excel,Csv,我将文件夹中的所有CSV文件合并到一个Excel工作表中 子合并文件\u单击() 将strSourcePath设置为字符串 作为字符串的Dim strdespath 作为字符串的Dim strFile 将strData设置为字符串 Dim x作为变体 暗淡的碳纳米管 变暗,变长 尺寸c与长度相同 Application.ScreenUpdating=False strSourcePath=Sheet1.范围(“G2”).值 如果正确(strSourcePath,1)“\”则strSourcePa
子合并文件\u单击()
将strSourcePath设置为字符串
作为字符串的Dim strdespath
作为字符串的Dim strFile
将strData设置为字符串
Dim x作为变体
暗淡的碳纳米管
变暗,变长
尺寸c与长度相同
Application.ScreenUpdating=False
strSourcePath=Sheet1.范围(“G2”).值
如果正确(strSourcePath,1)“\”则strSourcePath=strSourcePath&“\”
strFile=Dir(strSourcePath&“*.csv”)
当Len(strFile)>0时执行
Cnt=Cnt+1
如果Cnt=1,则
r=6
其他的
r=单元格(行数,“A”)。结束(xlUp)。行数+1
如果结束
打开strSourcePath&strFile以作为#1输入
直到EOF(1)为止
行输入#1,标准数据
x=拆分(标准数据,“,”)
对于c=0到UBound(x)
单元格(r,c+1)。值=修剪(x(c))
下一个c
r=r+1
环
关闭#1
strFile=Dir
环
Application.ScreenUpdating=True
如果Cnt=0,则_
MsgBox“未找到CSV文件…”,请感叹
端接头
这会将所有CSV文件合并到一个工作表中,但每个CSV文件的顶部都有一个标题和其他信息,占据12行
我希望保留第一个CSV的12行,但在放入Excel工作表之前,将其从其余文件中删除
我希望文件显示为一个文件,而不是看起来像是复制并粘贴到工作表上的文件。对现有代码的最简单更改是,如果
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
子合并文件\u单击()
将strSourcePath设置为字符串
作为字符串的Dim strdespath
作为字符串的Dim strFile
将strData设置为字符串
Dim x作为变体
暗淡的碳纳米管
变暗,变长
尺寸c与长度相同
暗输入与长输入相同
Application.ScreenUpdating=False
strSourcePath=Sheet1.范围(“G2”).值
如果正确(strSourcePath,1)“\”则strSourcePath=strSourcePath&“\”
strFile=Dir(strSourcePath&“*.csv”)
当Len(strFile)>0时执行
Cnt=Cnt+1
如果Cnt=1,则
r=6
其他的
r=单元格(行数,“A”)。结束(xlUp)。行数+1
如果结束
打开strSourcePath&strFile以作为#1输入
inputRow=0
直到EOF(1)为止
行输入#1,标准数据
'保存已读取的行数
inputRow=inputRow+1
'仅当这是第一个文件或我们有
已经过了第12排
如果Cnt=1或inputRow>12,则
x=拆分(标准数据,“,”)
对于c=0到UBound(x)
单元格(r,c+1)。值=修剪(x(c))
下一个c
r=r+1
如果结束
环
关闭#1
strFile=Dir
环
Application.ScreenUpdating=True
如果Cnt=0,则_
MsgBox“未找到CSV文件…”,请感叹
端接头
正如Yow E3K所说,您只需第一次复制前12行即可。
我的偏好是首先将它们放在模板上,然后再不复制它们
下面的代码(来自-thank you Chancea)已经修改了一半,通过输入
.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
您知道正在处理的数据,但请注意,CSV格式通常允许在文字字符串中嵌入逗号(用双引号括起来的字段“像这样”)。如果你得到了其中的任何一个,你的代码都会失败。@RichHolton所以在测试之后,我发现了一些实例,这些实例导致了问题。我能做些什么来避免这个问题?你可能会发现这个问题/答案很有帮助:@RichHolton我浏览了那篇文章,抓取了使用QueryTables导入csv的代码,并对其进行了测试,导入一个文件效果很好。我怎样才能重新安排,继续抓取文件夹中的每个csv,以实现我的目标?为什么第2行,OP提到12行重复数据?您需要区分第一个文件和其他文件,还建议在
之后删除查询表
。刷新
,否则工作簿的大小可能会变得太大。