[英]VBA : Combine multiple Workbooks(with mutiple Worksheets) into One Workbook with Data One Below The Other
I'm new to VBA.我是 VBA 的新手。 We are trying to do the following:我们正在努力做到以下几点:
We would like to write a VBA code which would do the following in a separate workbook called Output.xlsm我们想编写一个 VBA 代码,它将在名为 Output.xlsm 的单独工作簿中执行以下操作
In Output.xlsm-> Sheet1:在 Output.xlsm-> Sheet1 中:
Copy all data from Workbook1->Sheet1 including header.从 Workbook1->Sheet1 复制所有数据,包括标题。
Copy all data from Workbook2->Sheet1 not including header.从 Workbook2->Sheet1 复制所有数据,不包括标题。
Copy all data from Workbook3->Sheet1 not including header.从 Workbook3->Sheet1 复制所有数据,不包括标题。 until Workbook n.直到工作簿 n。
Same as above for all other sheets in Output.xlsm . Output.xlsm 中的所有其他工作表与上述相同。 ie, Output.xlsm-> Sheet2:即,Output.xlsm-> Sheet2:
Copy all data from Workbook1->Sheet2 including header.从 Workbook1->Sheet2 复制所有数据,包括标题。
Copy all data from Workbook2->Sheet2 not including header.从 Workbook2->Sheet2 复制所有数据,不包括标题。
Copy all data from Workbook3->Sheet2 not including header.从 Workbook3->Sheet2 复制所有数据,不包括标题。 until Workbook n.直到工作簿 n。
Maintain the SheetNames.维护 SheetNames。
We tried this code below which we researched, but it combines all data from all workbooks and all worksheets into one single sheet, and the combining of data does not remove the headers etc. Kindly discount this code below as we are beginners in VBA.我们在我们研究的下面尝试了这段代码,但它将所有工作簿和所有工作表中的所有数据合并到一张表中,并且数据的合并不会删除标题等。由于我们是 VBA 初学者,请在下面打折这段代码。
Sub simpleXlsMerger()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("C:\consolidated\")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
Range("A2:IV" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
End Sub
We have tried to research many posts in StackOverFlow.我们试图研究 StackOverFlow 中的许多帖子。 Would you please guide us on how to complete this.请您指导我们如何完成此操作。
Example Workbooks:示例工作簿:
Can you try this?你能试试这个吗?
I haven't looked as your files so some adjustments may be needed.我没有查看您的文件,因此可能需要进行一些调整。
Sub simpleXlsMerger()
Dim bookList As Workbook, bFirst As Boolean, ws As Worksheet, wsO As Worksheet
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Dim rCopy As Range
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("C:\consolidated\")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
For Each ws In bookList.Worksheets
If Not bFirst Then
Set wsO = ThisWorkbook.Worksheets.Add()
wsO.Name = ws.Name
Set rCopy=ws.range("A1").currentregion
'Set rCopy = ws.Range("A1", ws.Range("IV" & Rows.Count)).End(xlUp)
Else
Set wsO = ThisWorkbook.Worksheets(ws.Name)
Set rCopy=ws.range("A1").currentregion
Set rCopy=rcopy.offset(1).resize(rcopy.rows.count-1)
'Set rCopy = ws.Range("A2", ws.Range("IV" & Rows.Count)).End(xlUp)
End If
rCopy.Copy wsO.Range("A" & Rows.Count).End(xlUp)(2)
Next ws
bookList.Close
bFirst = True
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.