有n个excel表,每个表包含n个工作表。 需要: 需要使用宏将每个excel的所有工作表按升序组合成单个excel
答案 0 :(得分:0)
我认为你的意思是Excel工作簿,每个工作表都包含多个工作表?如果是这种情况,请右键单击要复制到新目标的选项卡,单击“移动或复制...”并按照说明复制到新书。如果要将工作表保留在原始位置以及制作副本,请务必选中“创建副本”框。
为了清楚起见,您可以为此新工作簿命名并移动或复制其他选项卡。
答案 1 :(得分:0)
这应该可以满足您的需求,它允许您选择文件夹,然后遍历其中的所有文件,并将这些工作簿中的所有工作表合并到保存在同一目录中的单个工作表中。虽然没有排序,但你可以find multiple ready macros这样做,所以只需在结果上运行一个或者在我的wb2之前粘贴他们的内容save&关闭。
Sub MergeFilesWithoutSpaces()
Dim fldr As FileDialog
Dim path As String
'pick folder
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then Exit Sub
path = .SelectedItems(1)
End With
'create new workbook to store results and rename its sheet
With Application
.EnableEvents = False
.ScreenUpdating = False
.SheetsInNewWorkbook = 1
End With
Set wb2 = Workbooks.Add
wb2.Worksheets(1).Name = "00Delete_Me"
'fetch filename
Filename = Dir(path & "\*.xls", vbNormal)
'loop through all *XLS files
If Len(Filename) = 0 Then Exit Sub
Do Until Filename = vbNullString
Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
'copy each worksheet
For Each Sh In Wkb.Sheets
Sh.Copy After:=wb2.Sheets(wb2.Sheets.Count)
Next Sh
Wkb.Close False
Filename = Dir()
Loop
'delete the initial sheet and save result as "combined" workbook in same directory
Application.DisplayAlerts = False
wb2.Sheets("00Delete_Me").Delete
wb2.SaveAs Filename:=(path & "\combined"), FileFormat:=xlOpenXMLWorkbookMacroEnabled
Application.DisplayAlerts = True
'close combined
wb2.Close
End Sub