If Two Cells Match Insert Data Two Rows Down - Help

jbenfrancis1

New Member
Joined
Feb 22, 2022
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi, i have done a lot of searching and found some useful stuff but cannot piece it together to make it work. Here is my dilemma.

Sheet1 has data on it and the corresponding date.
Sheet2 has historical data on it with dates.

Sheet2 has column headers for each day so for example C6 is 2/21/2022, D6 is 2/20/2022 and so on all the way out to BJ6.

I am trying to write some code that will look at the date on Sheet1 and search for it on Sheet2 row 6. If they match, then add the data from Sheet1 2 rows below the date on Sheet2

This code allow it to search the range correctly and cycle through
VBA Code:
Dim b As Range
Set b = Sheets("Sheet2").Range("C6:BJ6")
For Each c In Sheets("Sheet1").Range("C2")
    If IsNumeric(Application.Match(c, b, 0)) Then
       'I don't know what to put here to say stop and insert data once match is found
        Exit Sub
        End If
    Next c
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi, i have done a lot of searching and found some useful stuff but cannot piece it together to make it work. Here is my dilemma.

Sheet1 has data on it and the corresponding date.
Sheet2 has historical data on it with dates.

Sheet2 has column headers for each day so for example C6 is 2/21/2022, D6 is 2/20/2022 and so on all the way out to BJ6.

I am trying to write some code that will look at the date on Sheet1 and search for it on Sheet2 row 6. If they match, then add the data from Sheet1 2 rows below the date on Sheet2
Can you provide some details about the data from sheet1 you want to add to sheet2 when a match is found? Perhaps you can post some sample data for sheet1 using XL2BB.
 
Upvote 0
This is an example, to pull the data from sheet1 over to sheet2 only if the date on sheet1 matches. Sheet1 will have new data in it everyday (yesterdays data) and a new column is added to shet2 with yesterdays date. Trying to use macros though so when we have long weekends and holidays it will match dates and add in correct spots

Example .xlsx
BCDEFGH
221-Feb20-Feb19-Feb
3Book
4Author
5ISBN
Sheet2



Example .xlsx
ABCDE
1
2Date2/21/2022
3
4
5BookThe Wild
6Author J. Wild
7ISBN9059635412
Sheet1
 
Upvote 0
In your OP you indicated the dates in Sheet2 are in row 6, but that's not what your sample shows. Are the dates on sheet2 in merged cells or are they just centered across two adjacent cells?
 
Upvote 0
In your OP you indicated the dates in Sheet2 are in row 6, but that's not what your sample shows. Are the dates on sheet2 in merged cells or are they just centered across two adjacent cells?
Yeah sorry, They should be in row six let me send you an upodated version. And yes C and D are merger, thats the formatting of the book I am working in


Example .xlsx
ABCDEFGH
1
2
3
4
5
621-Feb20-Feb19-Feb
7Book
8Author
9ISBN
Sheet2
 
Upvote 0
Example .xlsx
ABCDEFGH
1
2
3
4
5
621-Feb20-Feb19-Feb
7Book
8Author
9ISBN
Sheet2


Had to merge everything correct.
 
Upvote 0
I'm still a bit confused - thought you wanted the data from col C in sheet1 to be placed two rows below the matching date in sheet2??
 
Upvote 0
Alright let me give this one last try, sorry I should read my own questions better. Here is a fresh booklet of each. I would like data in sheet1 to file into sheet2 based off date matching. Let me know if you need additional info. These should exactly how my sheets are layed out.


Example .xlsx
ABCD
1
2Date2/21/2022
3
4VolumePercent
5Book 110057.14
6Book 2 5028.57
7Book 32514.28
Sheet1


Example .xlsx
ABCDEFGH
1
2
3
4
5
621-Feb20-Feb19-Feb
7VolumePercentVolumePercentVolumePercent
8Book 1
9Book 2
10Book 3
Sheet2
 
Upvote 0
OK. Give this a try on a copy of your workbook.
VBA Code:
Sub jbenfrancis1()
Dim b As Range, Fnd As Variant
With Sheets("Sheet2")
    Set b = .Range("C6:BJ6")
    Fnd = Application.Match(CLng(Sheets("Sheet1").Range("C2")), b, 0)
    If IsError(Fnd) Then
        MsgBox "Date not found in sheet2, row6 - exiting sub"
        Exit Sub
    Else
        .Cells(8, Fnd + 2).Resize(3, 2).Value = Sheets("Sheet1").Range("B5:C7").Value
    End If
End With
End Sub
 
Upvote 0
Solution
OK. Give this a try on a copy of your workbook.
VBA Code:
Sub jbenfrancis1()
Dim b As Range, Fnd As Variant
With Sheets("Sheet2")
    Set b = .Range("C6:BJ6")
    Fnd = Application.Match(CLng(Sheets("Sheet1").Range("C2")), b, 0)
    If IsError(Fnd) Then
        MsgBox "Date not found in sheet2, row6 - exiting sub"
        Exit Sub
    Else
        .Cells(8, Fnd + 2).Resize(3, 2).Value = Sheets("Sheet1").Range("B5:C7").Value
    End If
End With
End Sub


Thank you for the help and for being patient! Works like a charm
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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