将同一文件夹中多个 Excel 文件的数据复制并粘贴到一个 Excel 文件中
Copy and Paste Data from Multiple Excel Files within the same folder into one Single Excel File
早上好!
我正在尝试遍历同一文件夹中的 Excel 个文件,以将一系列数据复制并粘贴到单个 Excel 工作簿中。
单元格范围的起始位置始终一致,它从单元格 D12 开始,结束于 C91 和 Z91.The table 之间的任意位置,但尺寸确实从 80 R x 2 C 到 80 R 不等x 22 C. 我试图从中提取数据的 Excel 文件有多个 sheets 但我需要数据的 sheet 总是具有相同的名称,在这种情况下它的“性能”。
我需要它
- 查找文件中的数据维度(table)
- 复制 table
- 粘贴到目的地(在上一个 table 下方)
- 循环到下一个文件
- 重复步骤 1-4
非常感谢您的帮助,感谢您的帮助,如果您有任何具体问题,请告诉我。
SO 不是代码编写服务,但昨天我做了类似的事情,你可以用它作为起点。
代码在一个工作簿中。它创建一个新工作簿(目标)并循环所有工作簿(源)的文件夹,并将工作表从这些文件夹复制到目标。
最后,保存目标:
Option Explicit
Public Function ImportSheets()
Dim Source As Excel.Workbook
Dim Target As Excel.Workbook
Const FolderName As String = "C:\Path\SomeFolder"
Const FileMask As String = "*.xlsx"
Const Separator As String = "\"
Const TargetMask As String = "Current Projects {0}.xlsx"
Dim Worksheet As Excel.Worksheet
Dim FileName As String
Dim Count As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Target = Workbooks.Add
FileName = Dir(FolderName & Separator & FileMask)
Do While FileName <> ""
Set Source = Workbooks.Open(FolderName & Separator & FileName)
For Each Worksheet In Source.Worksheets
Count = Target.Worksheets.Count
Source.Worksheets(Worksheet.Name).Copy After:=Target.Worksheets(Count)
Next
Source.Close False
FileName = Dir()
Loop
Set Source = Nothing
Target.Worksheets(1).Delete
FileName = Replace(TargetMask, "{0}", Format(Date, "yyyy-mm-dd"))
Target.SaveAs FolderName & Separator & FileName
Target.Close
Set Target = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Function
您应该能够稍微扩展它以仅复制源工作表的特定部分。
也许是这个?
Sub Basic_Example_1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("A1:C1")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With
'Set the destrange
Set destrange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
您可以在下面的 link 中找到其他类似示例。
早上好!
我正在尝试遍历同一文件夹中的 Excel 个文件,以将一系列数据复制并粘贴到单个 Excel 工作簿中。
单元格范围的起始位置始终一致,它从单元格 D12 开始,结束于 C91 和 Z91.The table 之间的任意位置,但尺寸确实从 80 R x 2 C 到 80 R 不等x 22 C. 我试图从中提取数据的 Excel 文件有多个 sheets 但我需要数据的 sheet 总是具有相同的名称,在这种情况下它的“性能”。
我需要它
- 查找文件中的数据维度(table)
- 复制 table
- 粘贴到目的地(在上一个 table 下方)
- 循环到下一个文件
- 重复步骤 1-4
非常感谢您的帮助,感谢您的帮助,如果您有任何具体问题,请告诉我。
SO 不是代码编写服务,但昨天我做了类似的事情,你可以用它作为起点。
代码在一个工作簿中。它创建一个新工作簿(目标)并循环所有工作簿(源)的文件夹,并将工作表从这些文件夹复制到目标。
最后,保存目标:
Option Explicit
Public Function ImportSheets()
Dim Source As Excel.Workbook
Dim Target As Excel.Workbook
Const FolderName As String = "C:\Path\SomeFolder"
Const FileMask As String = "*.xlsx"
Const Separator As String = "\"
Const TargetMask As String = "Current Projects {0}.xlsx"
Dim Worksheet As Excel.Worksheet
Dim FileName As String
Dim Count As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Target = Workbooks.Add
FileName = Dir(FolderName & Separator & FileMask)
Do While FileName <> ""
Set Source = Workbooks.Open(FolderName & Separator & FileName)
For Each Worksheet In Source.Worksheets
Count = Target.Worksheets.Count
Source.Worksheets(Worksheet.Name).Copy After:=Target.Worksheets(Count)
Next
Source.Close False
FileName = Dir()
Loop
Set Source = Nothing
Target.Worksheets(1).Delete
FileName = Replace(TargetMask, "{0}", Format(Date, "yyyy-mm-dd"))
Target.SaveAs FolderName & Separator & FileName
Target.Close
Set Target = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Function
您应该能够稍微扩展它以仅复制源工作表的特定部分。
也许是这个?
Sub Basic_Example_1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("A1:C1")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With
'Set the destrange
Set destrange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
您可以在下面的 link 中找到其他类似示例。