How to Merge Excel Sheets Horizontally
Tags: vba, powerbi, excel, tutorial
Published: 2024-02-08 04:20:04

Horizontal merging is also a common Excel problem encountered in work, and it is also quite troublesome to do in Excel, and many people do not know how to do it. For example, how to do one-on-one, how to do one-to-many, how to do with different numbers of rows, and how to do with multiple merge conditions. This article provides detailed solutions for these scenarios. And the method here is definitely the most efficient one that you have found.

Let’s skip the formalities and get straight to business.

Firstly, we need to use a tool called esProc SPL, a software specifically designed to handle structured table data. It has powerful computing capabilities and is easy to use. After downloading, double-click to install. The examples in this article are all provided with source code and can be copied and pasted for use.

Download address: [esProc Desktop Download](http://www.scudata.com/download-Desktop)

## 1. One-on-one merge

As shown in the figure, there are two tables, namely the price table and the inventory table for certain meat products. Now, we need to merge the two tables horizontally.

Before merging:

Meats.xlsx

![Meats.xlsx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06lkqi7u7afzd1j6l1ft.png)


MeatStock.xlsx

![MeatStock.xlsx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8zj32juqfkluxgkw991.png)



**1) Horizontal merge, retain all rows (full join)**

Merge according to Name, and retain all rows after merging

After merging:

![Merged Meats and MeatStock sheets with all rows retained](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftlozb2ms3pt7agjlht9.png)


Implementation code:

```
	A
1	=file(“Meats.xlsx”).xlsimport@t()
2	=file(“MeatStock.xlsx”).xlsimport@t()
3	=join@f(A1:Price,Name;A2:Stock,Name)
4	=A3.new([Price.Name,Stock.Name].ifn():Name,Stock.Stock,Stock.MinimumStock,Price.UnitPrice)
5	=file(“MeatsPriceStock.xlsx”).xlsexport@t(A4)

```

A3 join@f() represents full join, retaining all rows

A4 The bold code means selecting non-null Name values


**2) Horizontal merge, only retaining duplicate rows (inner join)**

Merge according to Name, only retaining rows common to both files

After merging:

![Merged Meat and MeatStock sheets with rows that are common to both files retained](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irdf55q4w42p7rstp8g5.png)


Implementation code:

```
	A
1	=file(“Meats.xlsx”).xlsimport@t()
2	=file(“MeatStock.xlsx”).xlsimport@t()
3	=join(A1:Price,Name;A2:Stock,Name)
4	=A3.new(Stock.Name,Stock.Stock,Stock.MinimumStock,Price.UnitPrice)
5	=file(“MeatsPriceStock.xlsx”).xlsexport@t(A4)
```
A3 inner join, retaining common rows


**3）Only retain the rows of the first file (left join)**

Merge according to Name, retain the rows of the first file after merging

After merging:

![Merged Meat and MeatStok sheets with only rows of the first file retained](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nw7ach09a8pglq3jr7w.png)


Implementation code:

```
	A
1	=file(“Meats.xlsx”).xlsimport@t()
2	=file(“MeatStock.xlsx”).xlsimport@t()
3	=join@1(A1:Price,Name;A2:Stock,Name)
4	=A3.new(Price.Name,Stock.Stock,Stock.MinimumStock,Price.UnitPrice)
5	=file(“MeatsPriceStock.xlsx”).xlsexport@t(A4)
```
A3 @1 is a left join, please note that here is the number 1, not the letter l


**4）Multiple merge conditions, only retaining the rows of the first file (left join)**

As shown in the following figure, according to the conditions of Region and Name, retain the rows of the first file, and merge the two tables horizontally.

Before merging:

![File A](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22kubiotx7afibty4yoc.png)

![File B](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0w9p86iypaw4p9ichu9h.png)

After merging:

![Horizontally merged File A and File B with rows of File A retained](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8unh0hp1y2hbcaz4y804.png)

Implementation code:

```
	A
1	=file(“Meats.xlsx”).xlsimport@t()
2	=file(“MeatStock.xlsx”).xlsimport@t()
3	=join@1(A1:Price,Region,Name;A2:Stock,Region,Name)
4	=A3.new(Price.Region,Price.Name,Stock.Stock,Stock.MinimumStock,Price.UnitPrice)
5	=file(“MeatsPriceStock.xlsx”).xlsexport@t(A4)
```
A3 can implement multi-condition merging by adding condition field names to the join() function

## 2. One-to-many merge

Before merging:

Types.xlsx

![Types.xlsx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nicjbexjjbv8n50qefhy.png)

Foods.xlsx

![Food.xlsx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9u1kculx342n2ho1jlu0.png)


After merging:

![Merged Types and Food files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqzl4ajf02ga839o4rxt.png)

It can be implemented using full join.

Implementation code:

```
	A
1	=T(“Types.xlsx”)
2	=T(“Foods.xlsx”)
3	=join@f(A1:Type,Type;A2:Food,Type)
4	=A3.new(Food.Type,Food.Name,Food.UnitPrice,Type.Description)
5	=T(“FoodsDescription.xlsx”,A4)
```
A3 @f is a full join.

If the description of the major categories Fruits and Meats is expected only to appear once, as shown in the figure

After merging:

![Merged Types and Foods files with duplicate description cells deleted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pq23fw8ybyn3s42vf3jt.png)

You can use the align() function.

Implementation code:

```
	A
1	=T(“Types.xlsx”)
2	=T(“Foods.xlsx”)
3	=A1.align(A2:Type,Type)
4	=A2.new(Type,Name,UnitPrice,A3(#).Description)
5	=T(“FoodsDescription.xlsx”,A4)
```
A3 align indicates that A1 is aligned to A2, with the alignment criteria being the Type column of A2 and the Type column of A1. If A2 has duplicate data, only the first row is aligned.

Using SPL, complex Excel operations can be done with just a few lines of code.

In addition, the syntax of SPL functions is simple, in line with natural logical thinking, and it is not difficult to understand.

Of course, the functions of SPL are not limited to this, and it is not a problem for SPL to execute Excel operations in various complex scenarios.

For those in need, you can refer to [Desktop and Excel Data Processing Cases](http://c.scudata.com/article/1651916536524). Answers to ninety percent of Excel problems in the workplace can be found in this book. The code in the book can basically be copied and used with slight modifications.

esProc Desktop is absolutely the tool in the hand and always FREE & EASY to download and apply: http://www.scudata.com/esproc-desktop/.