macro VBA - find matching values in columns between 2 sheets and return value in another column

tikcr

New Member
Joined
Dec 20, 2022
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone,
First of all, Happy New Year!

I would like to kindly ask for your support with this one. I have 2 excel sheets - sheet1 and sheet2.

I would like to check if the values from sheet1, Column D (starting from cell D2 and all below) exists somewhere in Column S in sheet2.
If yes, then return all matching rows from sheet2, Column S, Column G and Column I into the corresponding rows of Column F, Column G and Column H in sheet1.

I'd really appreciate your support with this one!
Many thanks!

Here is the data I am using:
sheet1

workbook.xlsx
ABCDEFGH
1Sales IDIDBruttoNetto
21
3100
4500
5800
6955
755
sheet1


sheet2

workbook.xlsx
ABCDEFGHIJKLMNOPQRS
1BruttoNettoID
249,4144,471
3668,85601,961
458,2452,4225
59,88,8236
60,680,6115
7300,55270,4955
83,993,5922
9351,21316,0955
105803,055222,74100
114994,944495,451
1242,6738,465
1326,423,7690
sheet2
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
then return all matching rows from sheet2, Column S, Column G and Column I into the corresponding rows of Column F, Column G and Column H in sheet1
How do you want the result on sheet1?
For example, for the first ID: 1, in sheet2 you have three records, how do you want those 3 records in sheet1, you can put the example of the desired result.
 
Upvote 0
Hi DanteAmor, thanks for your support.
I think the best would be to first look for first value from cell D2 (sheet1), if it is also in column S of sheet2, then return all those values.
Once it's done, check the next cells - D3, D4, etc.

Here is an example of how the results may be presented:

workbook.xlsx
ABCDEFGH
1Sales IDIDBruttoNetto
21149,4144,47
31001668,85601,96
450014994,944495,45
58001005803,055222,74
695555300,55270,49
75555351,21316,09
sheet1
 
Upvote 0
Try this:

VBA Code:
Sub find_matching_values()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim c As Range, r As Range, f As Range
  Dim cell As String
  Dim i As Long
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  Set r = sh2.Range("S:S")
  i = 2
  
  For Each c In sh1.Range("D2", sh1.Range("D" & Rows.Count).End(xlUp))
    Set f = r.Find(c.Value, , xlValues, xlWhole)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        sh1.Range("F" & i).Value = sh2.Range("S" & f.Row).Value
        sh1.Range("G" & i).Value = sh2.Range("G" & f.Row).Value
        sh1.Range("H" & i).Value = sh2.Range("I" & f.Row).Value
        i = i + 1
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  Next
End Sub
 
Upvote 0
Solution
Thank you very much! I will do some more tests later on, but on a first sight this is what I needed!
Really appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,215,089
Messages
6,123,058
Members
449,091
Latest member
ikke

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