Combining repeated rows from 2 sheets' data based on a key

pranavshandilya

New Member
Joined
Oct 9, 2013
Messages
4
I am having 2 sheets in excel and I am trying to combine the data such that for every record in Sheet1, I get records from Sheet2 based on the common key. Below is the sample data to clarify:

1598875276711.png


I have SHEET 1 and SHEET 2 Data and I want to generate something like SHEET 3. Can you please suggest how can I do it.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello,

Does this work as expected?

VBA Code:
Sub COPY_TO_SHEET_3()
    Application.ScreenUpdating = False
    With Sheets("Sheet1")
        For MY_ROWS = 2 To .Range("B" & Rows.Count).End(xlUp).Row
            MY_TEXT_A = .Range("A" & MY_ROWS).Value
            MY_TEXT_B = .Range("B" & MY_ROWS).Value
            Sheets("Sheet2").Select
            Range("A1:C" & Range("C" & Rows.Count).End(xlUp).Row).Select
            Selection.AutoFilter Field:=1, Criteria1:=MY_TEXT_B
            Range("A2:C" & Range("C" & Rows.Count).End(xlUp).Row).Select
            Selection.SpecialCells(xlCellTypeVisible).Copy
            With Sheets("Sheet3")
                .Range("C" & .Range("C" & Rows.Count).End(xlUp).Offset(1, 0).Row).PasteSpecial (xlPasteAll)
                .Range("A" & .Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row).Value = MY_TEXT_A
                .Range("B" & .Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Row).Value = MY_TEXT_B
            .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row & ":B" & .Range("A" & Rows.Count).End(xlUp).Row).Copy
            .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row & ":A" & .Range("C" & Rows.Count).End(xlUp).Row).PasteSpecial (xlPasteAll)
            End With
            Sheets("Sheet2").Range("A1:C1").AutoFilter
        Next MY_ROWS
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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