读取多个CSV文件,并将每个文件内容作为单独的表格写入excel电子表格
需要从folder1中读取多个CSV文件(例如file1、file2..),并将每个文件作为单独的表格写入目标excel。此外,从folder2中读取类似的CSV文件(例如,文件1、文件2..),并将每个CSV文件写入已存在图纸中的上述excel ****来源**** 第1页: 文件1:读取多个CSV文件,并将每个文件内容作为单独的表格写入excel电子表格,excel,vba,Excel,Vba,需要从folder1中读取多个CSV文件(例如file1、file2..),并将每个文件作为单独的表格写入目标excel。此外,从folder2中读取类似的CSV文件(例如,文件1、文件2..),并将每个CSV文件写入已存在图纸中的上述excel ****来源**** 第1页: 文件1: col1, col2, col3 A, B, C 1, 2, 3 col1, col2, col3 D, E, F 1, 2, 3 文件2: col1, col2, col3 U, V, W 4, 5, 6
col1, col2, col3
A, B, C
1, 2, 3
col1, col2, col3
D, E, F
1, 2, 3
文件2:
col1, col2, col3
U, V, W
4, 5, 6
col1, col2, col3
X, Y, Z
4, 5, 6
第2页:
文件1:
col1, col2, col3
A, B, C
1, 2, 3
col1, col2, col3
D, E, F
1, 2, 3
文件2:
col1, col2, col3
U, V, W
4, 5, 6
col1, col2, col3
X, Y, Z
4, 5, 6
****目标****
卓越:
表1(文件1):
表2(文件2):
代码:
由于所有CSV文件内容都将写入一张工作表而不是单个工作表,因此不确定如何将其复制到其余的CSV文件中。假设您有一个文件夹包含多个CSV文件,另一个文件夹包含相同数量的同名文件。因此,您可以通过以下方式处理所有文件:
Dim folder1 as string, folder2 as string
Dim fname as string ' current filename
Dim sh as Worksheet
Set wb = Workbooks.Add
If Err.Number <> 0 Then
' handle error here
End If
wb.SaveAs Filename:=destFolderPath & destFileName
If Err.Number <> 0 Then
' handle error here
End If
fname = Dir(folder1 & Application.PathSeparator & "*.csv")
If Err.Number <> 0 Then
' handle error here
ElseIf fname = vbNullString Then
' no CSV file exists in folder 1
Else
Do
Open folder1 & Application.PathSeparator & fName For Input As #1
If Err.Number <> 0 Then
' handle error here
End If
Open folder2 & Application.PathSeparator & fName For Input As #2
If Err.Number <> 0 Then
' handle error here
End If
' input files are open, add destination sheet
Set sh = wb.Sheets.Add(after:=wb.Sheets(Sheets.Count)) ' add new sheet
If Err.Number <> 0 Then
' handle error here
End If
sh.Name = Split(fname, ".")(0) ' rename sheet to name of current file w/o ".CSV"
' at this point you can fill dest file with header and data
' replace wb.Worksheets("Sheet1").Range("E" & row_count) kind of references with sh.Range("E" & row_count)
Close #1
Close #2
fname = Dir ' get next file from folder 1
Loop Until sFile = vbNullString ' until file exists
End If
Dim folder1作为字符串,folder2作为字符串
Dim fname作为字符串的当前文件名
将sh设置为工作表
设置wb=工作簿。添加
如果错误号为0,则
'在这里处理错误
如果结束
wb.SaveAs文件名:=destFolderPath和destFolderPath
如果错误号为0,则
'在这里处理错误
如果结束
fname=Dir(folder1&Application.PathSeparator&“*.csv”)
如果错误号为0,则
'在这里处理错误
ElseIf fname=vbNullString然后
'文件夹1中不存在CSV文件
其他的
做
打开folder1&Application.PathSeparator&fName,输入为#1
如果错误号为0,则
'在这里处理错误
如果结束
打开folder2&Application.PathSeparator&fName,输入为#2
如果错误号为0,则
'在这里处理错误
如果结束
'输入文件已打开,请添加目标工作表
设置sh=wb.Sheets.Add(之后:=wb.Sheets(Sheets.Count))'添加新工作表
如果错误号为0,则
'在这里处理错误
如果结束
sh.Name=Split(fname,“.”(0)”将工作表重命名为当前文件的名称w/o“.CSV”
'此时,您可以用头和数据填充dest文件
'将wb.工作表(“Sheet1”).范围(“E”和行数)类引用替换为sh.Range(“E”和行数)
关闭#1
关闭#2
fname=Dir'从文件夹1获取下一个文件
循环直到sFile=vbNullString'为止,直到文件存在为止
如果结束
备注:
每次文件/工作簿/工作表操作后,都需要检查Err.Number
是否成功
新工作簿将具有默认的工作表数。使用上述算法,这些表将保持为空。所有添加的图纸都将具有源文件的名称
sh.Cells(row\u count,5)
在这种情况下,引用类型可能比sh.Range(“E”&row\u count)
更方便,假设您有一个包含大量CSV文件的文件夹,另一个文件夹包含相同数量的同名文件。因此,您可以通过以下方式处理所有文件:
Dim folder1 as string, folder2 as string
Dim fname as string ' current filename
Dim sh as Worksheet
Set wb = Workbooks.Add
If Err.Number <> 0 Then
' handle error here
End If
wb.SaveAs Filename:=destFolderPath & destFileName
If Err.Number <> 0 Then
' handle error here
End If
fname = Dir(folder1 & Application.PathSeparator & "*.csv")
If Err.Number <> 0 Then
' handle error here
ElseIf fname = vbNullString Then
' no CSV file exists in folder 1
Else
Do
Open folder1 & Application.PathSeparator & fName For Input As #1
If Err.Number <> 0 Then
' handle error here
End If
Open folder2 & Application.PathSeparator & fName For Input As #2
If Err.Number <> 0 Then
' handle error here
End If
' input files are open, add destination sheet
Set sh = wb.Sheets.Add(after:=wb.Sheets(Sheets.Count)) ' add new sheet
If Err.Number <> 0 Then
' handle error here
End If
sh.Name = Split(fname, ".")(0) ' rename sheet to name of current file w/o ".CSV"
' at this point you can fill dest file with header and data
' replace wb.Worksheets("Sheet1").Range("E" & row_count) kind of references with sh.Range("E" & row_count)
Close #1
Close #2
fname = Dir ' get next file from folder 1
Loop Until sFile = vbNullString ' until file exists
End If
Dim folder1作为字符串,folder2作为字符串
Dim fname作为字符串的当前文件名
将sh设置为工作表
设置wb=工作簿。添加
如果错误号为0,则
'在这里处理错误
如果结束
wb.SaveAs文件名:=destFolderPath和destFolderPath
如果错误号为0,则
'在这里处理错误
如果结束
fname=Dir(folder1&Application.PathSeparator&“*.csv”)
如果错误号为0,则
'在这里处理错误
ElseIf fname=vbNullString然后
'文件夹1中不存在CSV文件
其他的
做
打开folder1&Application.PathSeparator&fName,输入为#1
如果错误号为0,则
'在这里处理错误
如果结束
打开folder2&Application.PathSeparator&fName,输入为#2
如果错误号为0,则
'在这里处理错误
如果结束
'输入文件已打开,请添加目标工作表
设置sh=wb.Sheets.Add(之后:=wb.Sheets(Sheets.Count))'添加新工作表
如果错误号为0,则
'在这里处理错误
如果结束
sh.Name=Split(fname,“.”(0)”将工作表重命名为当前文件的名称w/o“.CSV”
'此时,您可以用头和数据填充dest文件
'将wb.工作表(“Sheet1”).范围(“E”和行数)类引用替换为sh.Range(“E”和行数)
关闭#1
关闭#2
fname=Dir'从文件夹1获取下一个文件
循环直到sFile=vbNullString'为止,直到文件存在为止
如果结束
备注:
每次文件/工作簿/工作表操作后,都需要检查Err.Number
是否成功
新工作簿将具有默认的工作表数。使用上述算法,这些表将保持为空。所有添加的图纸都将具有源文件的名称
sh.Cells(row\u count,5)
在这种情况下,引用类型可能比sh.Range(“E”和row\u count)
更方便。这对我来说很好
Sub CombineCsvFiles()
'updateby Extendoffice 20151015
Dim xFilesToOpen As Variant
Dim I As Integer
Dim xWb As Workbook
Dim xTempWb As Workbook
Dim xDelimiter As String
Dim xScreen As Boolean
On Error GoTo ErrHandler
xScreen = Application.ScreenUpdating
Application.ScreenUpdating = False
xDelimiter = "|"
xFilesToOpen = Application.GetOpenFilename("Text Files (*.csv), *.csv", , "Kutools for Excel", , True)
If TypeName(xFilesToOpen) = "Boolean" Then
MsgBox "No files were selected", , "Kutools for Excel"
GoTo ExitHandler
End If
I = 1
Set xTempWb = Workbooks.Open(xFilesToOpen(I))
xTempWb.Sheets(1).Copy
Set xWb = Application.ActiveWorkbook
xTempWb.Close False
Do While I < UBound(xFilesToOpen)
I = I + 1
Set xTempWb = Workbooks.Open(xFilesToOpen(I))
xTempWb.Sheets(1).Move , xWb.Sheets(xWb.Sheets.Count)
Loop
ExitHandler:
Application.ScreenUpdating = xScreen
Set xWb = Nothing
Set xTempWb = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, , "Kutools for Excel"
Resume ExitHandler
End Sub
Sub-CombineCsvFiles()
'由扩展办公室更新20151015
Dim Xfilestopen作为变体
作为整数的Dim I
Dim xWb作为工作簿
Dim xTempWb作为工作簿
作为字符串的Dim xDelimiter
将xScreen设置为布尔值
关于错误转到错误处理程序
xScreen=Application.screen更新
Application.ScreenUpdating=False
xDelimiter=“|”
xFilesToOpen=Application.GetOpenFilename(“文本文件(*.csv),*.csv”,“Kutools for Excel”,True)
如果TypeName(xFilesToOpen)=“Boolean”,则
MsgBox“未选择任何文件”,“Kutools for Excel”
去出口
如果结束
I=1
设置xTempWb=Workbooks.Open(xfilestopen(I))
xTempWb.工作表(1).副本
设置xWb=Application.ActiveWorkbook
xTempWb.Close False
当我