Copy and Paste and Loop - Consolidate 2 worksheets

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
89
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

Effectively merging two sheets onto a new sheet where ID is the common identifier between them.

I am trying to create a macro which will copy a row from Tab 1 and paste it into Consolidated Tab, then go into Tab2 and find the rows that match the ID and paste the respective columns into Consolidated Tab.

It should be a loop and do this for all the Rows in Tab 1 . If it can’t find a match to copy over from Tab 2, it should just say “No Match to Raw Data” or "No Match" or something of that sort.

So three sheets being used in total:
*Tab 1 – Raw Data2 where ID is the key
*Tab 2 – Matrix sheet where ID matches to Raw Data2
*Consolidated sheet - where end output should be (ie the consolidation of data from RawData and Matrix)



Example:

In Tab 1 (Raw Data2 sheet) Start from A3 – take this and the respective columns and paste into Consolidated Sheet. ie ID 12345
1597342853792.png





Then find all the IDs 12345 in Tab 2 (Matrix sheet) and copy these over to Consolidated sheet
1597342872552.png



Consolidated Sheet should now look like below (left purple side is from Raw Data and right side is from Matrix):
1597342901805.png




End Output: Now do the same for all the remaining rows IDs in Tab 1 (Raw Data 2 sheet), where final output should look something like:
1597342953408.png




Note: no colour coding or formatting is needed in the macro, I just did this to try make easier to highlight what it is that I am trying to achieve.

A copy of the workbook can be found here Mapping Filter Doc

Would be ever so grateful for anyones support here. Thank you Gurus.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Open to any suggestions for what the macro should look like. Would be grateful for anyone's support.
 
Upvote 0
Using Power Query, which is an excel addin for version 2010. If you are using a later version, ie. 2016 or later, it can be found on the Data Tab and is now called Get and Transform. Bring each of the tables into PQ. Then join them on an left inner join to get the following.

Mapping Filter.xlsm
ABCDEFGHI
1IDSectionBeginning DateGeo Range StartGeo Range ToDescriptionTable2.NamesTable2.CountTable2.Concat
212345Lisbon8/1/2020ABacavsdvdfJohn3Nectarines, Apricots, Peaches
312345Lisbon8/1/2020ABacavsdvdfBarry2Nectarines, Peaches
412345Lisbon8/1/2020ABacavsdvdfTom3Nectarines, Apricots, Peaches
512380London8/1/2020ABasdfghSarah2Apricots, Peaches
612380London8/1/2020ABasdfghJane2Apricots, Peaches
712380London8/1/2020ABasdfghKevin1Apricots
812415Madrid8/1/2020ABafgdfhdgJack1Apricots
912415Madrid8/1/2020ABafgdfhdgJill1Apricots
1012450Paris8/6/2020BDxcvxcv
1112485Frankfurt8/11/2020BD vdbcsxcasGill3Nectarines, Apricots, Peaches
1212485Frankfurt8/11/2020BD vdbcsxcasBarry3Nectarines, Apricots, Peaches
1312485Frankfurt8/11/2020BD vdbcsxcasLuke3Nectarines, Apricots, Peaches
1412485Frankfurt8/11/2020BD vdbcsxcasAlex3Nectarines, Apricots, Peaches
1512520Munich8/16/2020BDcvxcbdfnbf
1612555Tokyo9/15/2020CCcvxcvxcb
1712590Delhi10/15/2020CCacavsdvdf
1812625New York11/14/2020CCasdfgh
1912660Lisbon12/14/2020ABafgdfhdg
2012695London1/13/2021ABxcvxcv
2112730Madrid2/12/2021AB vdbcsxcas
2212765Paris8/1/2020BBcvxcbdfnbf
2312800Frankfurt8/6/2020BBcvxcvxcb
2412835Munich8/11/2020BBacavsdvdf
2512870Tokyo8/16/2020CDasdfgh
2612905Delhi9/15/2020CDafgdfhdg
2712940New York10/15/2020CDxcvxcv
Sheet1
 
Upvote 0
Hi - thanks for the reply, will look into power query but ideally was hoping for a macro code to execute it I'm afraid.
 
Upvote 0
Try this:

VBA Code:
Sub Consolidate_2_worksheets()
  Dim a As Variant, b As Variant, c As Variant
  Dim dic As Object
  Dim i As Long, j As Long, k As Long, m As Long
  
  a = Sheets("Raw Data 2").Range("A3:F" & Sheets("Raw Data 2").Range("A" & Rows.Count).End(3).Row).Value2
  b = Sheets("Matrix").Range("A3:D" & Sheets("Matrix").Range("A" & Rows.Count).End(3).Row).Value2
  ReDim c(1 To UBound(a, 1) + UBound(b, 1), 1 To 10)
  Set dic = CreateObject("Scripting.Dictionary")
  
  For i = 1 To UBound(b, 1)
    If Not dic.exists(b(i, 1)) Then
      dic(b(i, 1)) = i
    End If
  Next
  
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 1)) Then
      k = k + 1
      For j = 1 To 6
        c(k, j) = a(i, j)
      Next
      c(k, 7) = "No Match"
    Else
      For m = dic(a(i, 1)) To UBound(b, 1)
        If b(m, 1) = a(i, 1) Then
          k = k + 1
          For j = 1 To 6
            c(k, j) = a(i, j)
          Next
          For j = 7 To 10
            c(k, j) = b(m, j - 6)
          Next
        Else
          Exit For
        End If
      Next
    End If
  Next
  
  Sheets("Consolidated").Range("A3").Resize(k, 10).Value = c
End Sub
 
Upvote 0
Here a smaller version, in case there are few records.

VBA Code:
Sub Consolidate_2_worksheets_2()
  Dim sh3 As Worksheet
  Dim c As Range, f As Range, lr As Long, n As Long

  Set sh3 = Sheets("Consolidated")
  For Each c In Sheets("Raw Data 2").Range("A3", Sheets("Raw Data 2").Range("A" & Rows.Count).End(3))
    Set f = Sheets("Matrix").Range("A:A").Find(c, , xlValues, xlWhole, , , False)
    n = WorksheetFunction.CountIf(Sheets("Matrix").Range("A:A"), c.Value)
    If n = 0 Then n = 1
    lr = sh3.Range("A" & Rows.Count).End(3).Row + 1
    sh3.Range("A" & lr).Resize(n, 6).Value = c.Resize(1, 6).Value
    If f Is Nothing Then sh3.Range("G" & lr) = "No Match" Else sh3.Range("G" & lr).Resize(n, 4).Value = f.Resize(1, 4).Value
  Next
End Sub
 
Upvote 0
Hi DanteAmor - nailed it! Thank you very much for this, both working as expected. Really appreciate it!! This will now enable to hopefully finish the overall macro! Thanks!
 
Upvote 0
Hi DanteAmor - i was going to ask if you could help explain your macro to me please? I get that it does what is needed, but I wanted to develop my knowledge on macros a bit more and try understand what the lines of code are doing. Would that be ok for you to explain?
 
Upvote 0
Would that be ok for you to explain?

With pleasure:
VBA Code:
Sub Consolidate_2_worksheets_2()
  Dim sh3 As Worksheet
  Dim c As Range, f As Range, lr As Long, n As Long

  Set sh3 = Sheets("Consolidated")
  'For each data in the range A3 and up to the last cell with data from column A.
  For Each c In Sheets("Raw Data 2").Range("A3", Sheets("Raw Data 2").Range("A" & Rows.Count).End(3))
    'Find the data in the Matrix sheet, column A
    Set f = Sheets("Matrix").Range("A:A").Find(c, , xlValues, xlWhole, , , False)
    'Counts the number of data that exists in the Matrix sheet column A
    n = WorksheetFunction.CountIf(Sheets("Matrix").Range("A:A"), c.Value)
    'If there is no data then n = 0, then I convert n = 1 to copy the row and put "No match"
    If n = 0 Then n = 1
    'Gets the last row with data from the Consolidate sheet and Add 1
    lr = sh3.Range("A" & Rows.Count).End(3).Row + 1
    'The consolidate sheet, in column A and in the last row,
    'resize the number of rows according to the value of n, that is,
    'the number of times it found the data and 6 columns.
    'In that range it receives the value of the sheet "raw data2"
    'In this case, it starts at the data 'c' and the number of rows grows according to n and 6 columns.
    sh3.Range("A" & lr).Resize(n, 6).Value = c.Resize(1, 6).Value
    'If 'f' is nothing then I add the text "no match" in column G.
    'Else, in column G put the values, in the same way the number of rows grows according to the value n and 4 columns.
    If f Is Nothing Then sh3.Range("G" & lr) = "No Match" Else sh3.Range("G" & lr).Resize(n, 4).Value = f.Resize(1, 4).Value
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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