我有很多擅长使用不同的图纸和相同的格式。是否有可用的插件将所有工作表组合到目标文件中的“合并”工作表中?
但是以下代码不能合并为1张纸
Sub GopFileExcel()
Dim FilesToOpen
Dim x As Integer
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="hMicrosoft Excel Files (*.xlsx), *.xlsx",
MultiSelect:=True, Title:="Files to Merg")
If TypeName(FilesToOpen) = “Boolean” Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
While x <= UBound(FilesToOpen)
Workbooks.Open Filename:=FilesToOpen(x)
Sheets().Move After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
答案 0 :(得分:0)
要做的第一件事是将所有工作表放入一个工作簿中。这是一些可以帮助您解决问题的代码。
Sub GetSheets()
Path = "C:\Users\USERNAME\Downloads\Test\" 'File path will all your separate files
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
然后,您可以编写一个循环以遍历所有工作表并将数据移动到一个工作表中。
答案 1 :(得分:0)
在@ Chrismas007答案之后,将所有工作表都放在一个工作簿中,然后可以使用下面的代码片段将所需范围复制到所需的目标工作表中。这会将您复制的部分追加到目标表中的下一个未使用的列。
Dim ws As Worksheets
Dim lastcol As Integer
For Each ws In Worksheets
ThisWorkbook.Worksheets(ws).Column(copyrange).Copy
lastcol = Worksheets(targetsheet).Cells(1, Columns.Count).End(xlToLeft).Column + 1
ThisWorkbook.Worksheets(targetsheet).Cells(1, lastcol).PasteSpecial
Paste:=xlPasteValues, operation:=xlNone
Next