Copy data from one sheet to another based on matching cells

cahedger

New Member
Joined
Jul 21, 2015
Messages
12
Hey Everybody,

So I am trying to make a macro that goes to the last row of sheet 1, takes the value in column F (PID)... and if it finds any matches in sheet 2 (column A)... copies the values in columns E, F, N & O (SID, Store Name, Total & Total Stock) from the corresponding row in sheet 2 and pastes it in a new row above the the matching PID in Column I - L (SID, Store, Total & Total Stock) of sheet 1. After that, it should continue up the spreadsheet repeating until it gets to the headers and then stop. I've included the sheets below... Thank you sooo much in advance for anybody that can help.


Sheet 1:

ABCDEFGHIJKL
ManagerVIDVendorOrder NameTotal GoalPIDStart DateEnd DateSIDStoreTotalTotal Stock
Jdoe34978Vendor 3Order Name 4500178647/20/20159/1/2015
Mfreeman36785Vendor 2Order Name 3250178636/17/20157/30/2015
Rdawson39867Vendor 4Order Name 225185347/15/20158/15/2015
Tcruise35487Vendor 1Order Name 1150115947/1/20159/1/2015

<tbody>
</tbody>

Sheet 2:

ABCDEFGHIJKLMNO
PIDProductVIDVendorSIDStorePriceMarginDailyDailyStockMonthlyMonthly StockMonthly %Total SalesTotal Stock
11594Product 135487Vendor 124876Store 1510%000002050
11594Product 135487Vendor 124698Store 2510%000001520
17863Product 236785Vendor 224678Store 31010%00000100100
17863Product 236785Vendor 221123Store 41015%000002730
11594Product 135487Vendor 121124Store 5510%000004150
17864Product 334978Vendor 322485Store 61510%000001520
17487Product 439867Vendor 426578Store 72015%0000087100
17487Product 439867Vendor 423754Store 82010%000005050
17864Product 334978Vendor 325746Store 91515%000007275
17864Product 334978Vendor 321212Store 101520%000001525
18534Product 539867Vendor 426589Store 112510%000001010

<tbody>
</tbody>
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You have more than one occurence of the PID number in sheet 2. How do you want to handle that? Take latest date only or copy values for all occurences and post to sheet 1?
 
Upvote 0
Thank you so much for the quick response... yes, if we can copy the values for all occurrences above the matching PID in sheet one that would be awesome. Thanks again :)
 
Upvote 0
Sorry... meant to say if we can paste the values for all occurrences above the the matching PID in sheet one.
 
Upvote 0
this seemed to do what you want. It tested without error, but you should test it just to be sure before applying it to your original file.

Code:
Sub CopyMoreStuff()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, i As Long, fn As Range, fAdr As String
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
lr = sh1.Cells(Rows.Count, 6).End(xlUp).Row
Set rng = sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
    For i = lr To 2 Step -1
        With sh1
            Set fn = rng.Find(.Cells(i, 6).Value, , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    fAdr = fn.Address
                    Do
                        .Rows(i).Insert
                        .Cells(i, 9) = sh2.Range("E" & fn.Row).Value
                        .Cells(i, 10) = sh2.Range("F" & fn.Row).Value
                        .Cells(i, 11) = sh2.Range("N" & fn.Row).Value
                        .Cells(i, 12) = sh2.Range("O" & fn.Row).Value
                        Set fn = rng.FindNext(fn)
                    Loop While fn.Address <> fAdr
                End If
        End With
    Next
End Sub
 
Upvote 0
Hey JLGWhiz... thanks again for your help and jumping in so quick. The code threw me an error at first, but once I added in "Rng As Range" into the variables it seemed to work great. Here's the edited code in case anybody else needs it.

Code:
Sub CopyMoreStuff()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, i As Long, fn As Range, fAdr As String, [B]Rng As Range[/B]
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
lr = sh1.Cells(Rows.Count, 6).End(xlUp).Row
Set Rng = sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
    For i = lr To 2 Step -1
        With sh1
            Set fn = Rng.Find(.Cells(i, 6).Value, , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    fAdr = fn.Address
                    Do
                        .Rows(i).Insert
                        .Cells(i, 9) = sh2.Range("E" & fn.Row).Value
                        .Cells(i, 10) = sh2.Range("F" & fn.Row).Value
                        .Cells(i, 11) = sh2.Range("N" & fn.Row).Value
                        .Cells(i, 12) = sh2.Range("O" & fn.Row).Value
                        Set fn = Rng.FindNext(fn)
                    Loop While fn.Address <> fAdr
                End If
        End With
    Next
End Sub

Thanks a million, you guys on this forum are the best.
 
Last edited:
Upvote 0
Hey JLGWhiz... thanks again for your help and jumping in so quick. The code threw me an error at first, but once I added in "Rng As Range" into the variables it seemed to work great. Here's the edited code in case anybody else needs it.

Code:
Sub CopyMoreStuff()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, i As Long, fn As Range, fAdr As String, [B]Rng As Range[/B]
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
lr = sh1.Cells(Rows.Count, 6).End(xlUp).Row
Set Rng = sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
    For i = lr To 2 Step -1
        With sh1
            Set fn = Rng.Find(.Cells(i, 6).Value, , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    fAdr = fn.Address
                    Do
                        .Rows(i).Insert
                        .Cells(i, 9) = sh2.Range("E" & fn.Row).Value
                        .Cells(i, 10) = sh2.Range("F" & fn.Row).Value
                        .Cells(i, 11) = sh2.Range("N" & fn.Row).Value
                        .Cells(i, 12) = sh2.Range("O" & fn.Row).Value
                        Set fn = Rng.FindNext(fn)
                    Loop While fn.Address <> fAdr
                End If
        End With
    Next
End Sub

Thanks a million, you guys on this forum are the best.

Sorry about that,
Regards, JLG
 
Upvote 0
Hey JLG,

No worries, that macro worked great. One (hopefully really easy) question... I'm putting that macro to use... but working on a new one as well. Could you tell me what part of that formula I would alter to make it insert a row below matches instead of above them... sorry I've spent hours trying to figure it out. Thanks again for all your help, I went ahead and added some likes to your posts.
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,018
Members
449,203
Latest member
tungnmqn90

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