combine data in three sheets based on the first three coulmn of each sheet.

shebrahimi

New Member
Joined
Dec 10, 2016
Messages
3
Dear Experts
I would appreciate if you could let me know how to merge the data in 3 sheets in a new sheet based on comparing the first 3 columns of each sheet. A simple form of each three sheet is as follows.

x1date2date1name
abc
abc
xyz
frt

<tbody>
</tbody>

y3y2y1date2date1name
abc
xyz
frt
frt
uop

<tbody>
</tbody>

z2z1date2date1name
xyz
xyz
frt
uop
frt

<tbody>
</tbody>

I want to merge them as follow

Thanks in advance
regards

z2z1y3y2y1x1date2date1name
abc
abc
frt
frt
xyz
xyz
uop

<tbody>
</tbody>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Comparison of the first three columns is unlikely, since your example layout indicates the first three columns of each sheet contain different type data. It is also unclear if the criteria is to execute a merge for a match, or mis-match, or other. A lot of guessing on this one.
 
Last edited:
Upvote 0
I used 3 test sheets (labelled "x", "y" and "z")
Excel Workbook
ABCD
1x1date2date1name
2x1.12-sep1-sepabc
3x1.26-sep5-sepabc
4x1.310-sep9-sepxyz
5x1.414-sep13-sepfrt
x


Excel Workbook
ABCDEF
1y3y2y1date2date1name
2y3.1y2.1y1.14-nov1-novabc
3y3.1y2.1y1.25-nov2-novxyz
4y3.1y2.2y1.36-nov3-novfrt
5y3.2y2.2y1.47-nov4-novfrt
6y3.2y2.3y1.58-nov5-novuop
y


Excel Workbook
ABCDE
1z2z1date2date1name
2z2.1z1.14-dec1-decxyz
3z2.2z1.17-dec5-decxyz
4z2.1z1.22-dec1-decfrt
5z2.3z1.38-dec5-decuop
6z2.3z1.49-dec7-decfrt
z


And a 4th sheet labelled "Merged" that, after running the mergeXYZ macro, looks like
Excel Workbook
ABCDEFGHI
1x1y3y2y1z2z1date2date1name
2x1.1*****2-9-20161-9-2016abc
3x1.2*****6-9-20165-9-2016abc
4*y3.1y2.1y1.1**4-11-20161-11-2016abc
5x1.4*****14-9-201613-9-2016frt
6*y3.1y2.2y1.3**6-11-20163-11-2016frt
7*y3.2y2.2y1.4**7-11-20164-11-2016frt
8****z2.1z1.22-12-20161-12-2016frt
9****z2.3z1.49-12-20167-12-2016frt
10*y3.2y2.3y1.5**8-11-20165-11-2016uop
11****z2.3z1.38-12-20165-12-2016uop
12x1.3*****10-9-20169-9-2016xyz
13*y3.1y2.1y1.2**5-11-20162-11-2016xyz
14****z2.1z1.14-12-20161-12-2016xyz
15****z2.2z1.17-12-20165-12-2016xyz
Merged


Go to the Visual Basic Environment (press ALT+F11), insert a module and paste this
Code:
Option Explicit

Dim rowM    As Long
Dim colM    As Long
Dim colMd2  As Long
Dim colMap  As Object

Private Sub addLabels(group As String)
    Dim sht As Worksheet
    Dim col As Long
    
    Set sht = ThisWorkbook.Worksheets(group)
    col = 1
    While Left(sht.Cells(1, col), 1) = group
        colM = colM + 1: shtM.Cells(1, colM) = sht.Cells(1, col)
        colMap.Item(shtM.Cells(1, colM).Value) = colM
        col = col + 1
    Wend
End Sub

Sub copyData(group As String)
    Dim sht     As Worksheet
    Dim col     As Long
    Dim colM    As Long
    Dim shtRow  As Long
    
    Set sht = ThisWorkbook.Worksheets(group)
    For shtRow = 2 To sht.UsedRange.Rows.Count
        'copy <group> columns
        col = 1
        While Left(sht.Cells(1, col), 1) = group
            colM = colMap.Item(sht.Cells(1, col).Value)
            shtM.Cells(rowM, colM).Value = sht.Cells(shtRow, col)
            col = col + 1
        Wend
        'copy date2, date1, name columns
        shtM.Cells(rowM, colMd2).Value = sht.Cells(shtRow, col) 'date2
        shtM.Cells(rowM, colMd2 + 1).Value = sht.Cells(shtRow, col + 1) 'date1
        shtM.Cells(rowM, colMd2 + 2).Value = sht.Cells(shtRow, col + 2) 'name
        rowM = rowM + 1
    Next shtRow
End Sub

Sub mergeXYZ()
    Dim i           As Integer
    Dim mergedArea  As Range
    Dim lastCol     As Long
    Dim lastRow     As Long
    
    shtM.UsedRange.ClearContents
    
    '----- setup columns -----
    
    Set colMap = CreateObject("scripting.Dictionary")
    
    colM = 0
    addLabels "x"
    addLabels "y"
    addLabels "z"
    
    colM = colM + 1: shtM.Cells(1, colM) = "date2"
    colMd2 = colM
    colM = colM + 1: shtM.Cells(1, colM) = "date1"
    colM = colM + 1: shtM.Cells(1, colM) = "name"
    
    '----- copy data -----
    
    rowM = 2
    copyData "x"
    copyData "y"
    copyData "z"
    
    Set colMap = Nothing
    
    '----- sort on name -----
    
    Set mergedArea = shtM.Range("A1").CurrentRegion
    lastCol = mergedArea.Columns.Count
    lastRow = mergedArea.Rows.Count
    shtM.Sort.SortFields.Clear
    shtM.Sort.SortFields.Add _
    Key:=shtM.Range(mergedArea.Cells(2, lastCol), mergedArea.Cells(lastRow, lastCol)) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With shtM.Sort
        .SetRange mergedArea
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
End Sub

press Fn4, this opens the properties pane (PP).
In the project explorer pane (EP) select sheet labelled "x". In PP change name to shtX.
In EP select sheet labelled "y". In PP change name to shtY.
In EP select sheet labelled "z". In PP change name to shtZ.
In EP select sheet labelled "Merged". In PP change name to shtM.
 
Upvote 0
Hi
I thank you very much for sharing your time and expertise. Indeed, some dates are common in the 3 separate sheets so that I want if the same names and dates exist in them, they will be matched against each other. Sorry, does it do this?
Thanks in advance,
Best regards
 
Upvote 0
if the same names and dates exist in them, they will be matched against each other.

There are two dates. How should they be the same? What are these two dates. Begindate and Enddate? Must they both be the same or overlap? Please tell us something more about what we are dealing with here?

What JGLWhiz also already noted, you are unclear in describing what you want to do. Give us more info. What do all these columns mean; where does the data come from; why a different number of columns in x,y and z.
 
Upvote 0
Hi. Thanks a lot. For example, date 1 and date2 for abc would be the same in X,Y and Z. In fact, date 1 is financial year and date 2 is when company release these data. X, Y and Z are respectively balance sheet, income statement and cash flow statement. I want for each company in each year all 3 statements will be in one worksheet. columns are different parts of those statements for example cash, receivables, sales, cost of goods sold, financial expenses, cash flow from operations, cash flow from investments and so on.
Best regards, Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,215,517
Messages
6,125,287
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top