This article will show you how to use Power Query to combine data stored in multiple Excel books into a single Excel book. In addition, VBA code for re-splitting the combined Excel book will also be presented.

Who should read this article

If you work at the head office and are following the steps below to receive budget requests from the various branches and respond with the allocated amounts, you should read this article.

  1. Cut and paste data from each Excel book into one (Combine)
  2. Enter the allocation amount for the requested amount
  3. To reply to each branch, copy the combined Excel book as many as the number of branches and manually delete the data of other branches (Split)

Until 2016, it was necessary to write difficult code in VBA to combine data.

However, Power Query was introduced in 2016, and VBA is no longer required to combine data.

The advantage of Power Query over VBA is that it is easy to operate.

Now, read the preconditions and sample files first, as I will show you tutorials.

Preconditions and Sample files

Preconditions

The preconditions for the tutorial introduced this time are as follows:

  • All excel books are saved in one folder.
  • The sheet name of each excel book with data entered is the same.

Sample files

Download it here.

DOWNLOAD

After unzipping the sample file, there are Excel books named "01_branchA.xlsx", "02_branchB.xlsx", and "03_branchC.xlsx" in the folder "test."

Every workbook has a sheet named “data.”

Each Excel book has data as shown in the figure below.

0A_branchA.xlsx
02_branchB.xlsx
03_branchC.xlsx

Tutorials

Overview of tutorials

You work at the head office and want to combine the excel books of budget requests sent from each branch, enter the allocated amounts, and then split the excel books by branch.

In order to implement these tasks, I will show you the following three tasks with the actual screen.

  1. Combine
  2. Enter
  3. Split

Combine using Power Query

First, let’s use Power Query to combine data stored in multiple Excel books.

Create a new Excel book and click "Get Data" at the left end of the tab "Data".

Then click “From File” and “From Folder.”

Open the unzipped folder.
Click "Combine & Load" from the pull-down menu of the "Combine" button at the bottom of the window.
Click "data" on the left side of the opened window, and click "OK" on the lower right side.
When the process is complete, you will see a table with the data from the three Excel books combined, as shown in the figure.
Once you learn how to do it, it's easier than cut-and-paste.

Enter the amount of allocation

Since the combined data is in a “table”, to enter additional data, simply enter it at the table end.

Let's enter "Allocation" in cell of E1.
When you enter text in E1, cells up to E10 are added to the table and their colors change.
Enter the amount you want to allocate from E2 to the cells E10.
I entered 90% of the requested amount as the allocated amount.
Next, let's split the excel book to reply to each branch.

Split using VBA

As far as I can tell, splitting an excel book is only possible with VBA.

Pressing Atl + F11 in Excel will open a VBA window.

Press Atl + F11 on the keyboard, and when the VBA window opens, click on "Module" in the menu "Insert".
As the standard module is added, copy and paste the code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Sub Split_Sht()
myArr = Cells(1, 1).CurrentRegion
maxRow = UBound(myArr)

Set myDic = CreateObject("Scripting.Dictionary")
For i = 2 To maxRow
If Not myDic.Exists(myArr(i, 1)) Then
myDic.Add myArr(i, 1), Null
End If
Next i
mySourceName = myDic.Keys

Worksheets(1).Cells(1, 1).Activate

Worksheets(1).ListObjects(1).TableStyle = ""

Dim myPath As String
MsgBox "Select a folder for save", vbExclamation
With Application.FileDialog(msoFileDialogFolderPicker)

.InitialFileName = ThisWorkbook.Path & "\"

If .Show = True Then

myPath = .SelectedItems(1)
Else

MsgBox "Canceled", vbExclamation
Exit Sub
End If
End With

For Each c In mySourceName

Worksheets(1).Activate
With ActiveSheet.Range("A1")
.AutoFilter Field:=1, Criteria1:=c
.CurrentRegion.Copy
End With

Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
Selection.PasteSpecial Paste:=xlPasteColumnWidths
ActiveSheet.Paste

Columns("A:A").Delete

ws.Name = "data"
ActiveSheet.Move
ActiveSheet.Cells(1, 1).Select
ActiveWorkbook.SaveAs myPath & c
ActiveWorkbook.Close
Next
With Worksheets(1)
.Activate
.ShowAllData
End With
Set myDic = Nothing
End Sub
After pasting, click the x in the upper right corner to close the VBA window.
Return to the Excel window, and click on "Macros" on the far right side of the tab "View."
When the macro window opens, click "Run" in the upper right corner.
Follow the dialog that appears and choose a folder to save the split workbook.
Open the folder you chose to save the file to.
Since the Excel Book has been created, let's try opening "01_branchA .xlsx."
"Allotment" has been added to column D.
This completes the Excel books that notify each branch of the allocated amount.

Postscript

This method allows for repeated combining and splitting.

In this article, I have only introduced the basic method of combining and splitting an Excel book.

In the future, I would like to introduce various countermeasures such as how to deal with cases where the sheet names are not the same.

Error handling when combining sheets with different names in Power Query

Error handling when combining sheets with different names in Power Query

One of the prerequisites for merging Excel books in a power query is that all sheets must have the same name. This article outlines the errors that occur when sheet names are different and how to deal