Você está na página 1de 2

Combine multiple workbooks to one workbook with VBA

For the skilled and professional programmers, you can use VBA scripts to combine multiple
workbooks into one master workbook. You can deal with this with the following steps:

1. Put all the workbooks that you want to combine into the same directory. See screenshot:

2. Launch an Excel file that you want to combine other workbooks into.

3. Click Developer > Visual Basic, a new Microsoft Visual Basic for
applications window will be displayed, click Insert > Module, and input the following code
into the Module:
Sub GetSheets()
1 Path = "C:\Users\dt\Desktop\dt kte\"
2 Filename = Dir(Path & "*.xls")
3 Do While Filename <> ""
4 Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
5 For Each Sheet In ActiveWorkbook.Sheets
6 Sheet.Copy After:=ThisWorkbook.Sheets(1)
7 Next Sheet
Workbooks(Filename).Close
8 Filename = Dir()
9 Loop
10 End Sub
11
12

Tip: In the above code, you can change the path to the one that you are using.

4. Then click button to run the code, and all of the worksheets (including the blank
worksheets) within the workbooks have been merged into the master workbook.

Note: This VBA code can merge the entire workbooks into the master workbook, if you want
to combine specified worksheets of the workbooks, this code will not work.

Você também pode gostar