VBA to match data from 2 sheets and combine it into 1 table (where additional rows will be added)

Rudy313

New Member
Joined
Dec 16, 2021
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I'm looking for a solution for my case.
Right now I have a file with 2 sheets where I have similar data.
In first sheet I have a Raw Data with all the food which restaurant sold and what components were used to create the dish:
1639641986143.png


I need to match the data from second sheet (screen below) based on "Component" column and add below missing rows with data what exactly was used to create the "Dip"

1639642041044.png


Basically, the macro should be able to match the data based on column B+A and copy below each food in sheet 1 all components which were used to do it.
My expectation is to get outcome as on screen below.

1639642142028.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi Rudy313,

Check below code:

VBA Code:
Sub compareAndCopy()

Application.ScreenUpdating = False
Dim foodNameComp As String, isAvailable As String
Dim lRowSh1 As Integer, lRowSh2 As Integer
Dim rowno1 As Integer, rowno2 As Integer

lRowSh1 = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lRowSh2 = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

nextRow = lRowSh1 + 1

For rowno2 = 2 To lRowSh2
    isAvailable = "N"
    For rowno1 = 2 To lRowSh1
        If Sheets("Sheet2").Range("A" & rowno2) & Sheets("Sheet2").Range("B" & rowno2) = Sheets("Sheet1").Range("A" & rowno1) & Sheets("Sheet1").Range("B" & rowno1) Then
            isAvailable = "Y"
            Exit For
        End If
    Next
    If isAvailable = "N" Then
        Sheets("Sheet2").Range("A" & rowno2 & ":D" & rowno2).Copy Sheets("Sheet1").Range("A" & nextRow)
        nextRow = nextRow + 1
    End If
Next
Application.ScreenUpdating = True
End Sub

Regards,
Saurabh
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,140
Members
448,551
Latest member
Sienna de Souza

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