[VBA] Copy value between sheets based on criteria

RobertHamberg

New Member
Joined
Jan 11, 2018
Messages
34
Office Version
  1. 365
I need a script that copy values from Sheet1 to Sheet2.

I need the script to search for matching rows in Sheet1 and Sheet2 based on the values in column A on Sheet1 (Sheet2 will have alot more rows then in this example) and then copy the values on those rows from column F,G and H in Sheet1 to the matching rows on Sheet2 AND on the matching column based on the values in cells F1, G1 and H1 on Sheet1. (These values will change as that are based on the WEEKNUM-function)


Sheet1
[TABLE="class: grid, width: 644"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[/TR]
[TR]
[TD]Name[/TD]
[TD]Owner[/TD]
[TD]Time[/TD]
[TD]Start[/TD]
[TD]End[/TD]
[TD]4[/TD]
[TD]5[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]Test6[/TD]
[TD]Resurs 7[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]50[/TD]
[/TR]
[TR]
[TD]Test7[/TD]
[TD]Resurs 8[/TD]
[TD="align: right"]88[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"]30[/TD]
[/TR]
</tbody><colgroup><col span="3"><col span="2"><col span="3"></colgroup>[/TABLE]

Sheet2
[TABLE="class: grid, width: 967"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD="align: right"]F[/TD]
[TD="align: right"]G[/TD]
[TD="align: right"]H[/TD]
[TD="align: right"]I[/TD]
[TD="align: right"]J[/TD]
[TD="align: right"]K[/TD]
[TD="align: right"]L[/TD]
[TD="align: right"]M[/TD]
[TD="align: right"]N[/TD]
[TD="align: right"]O[/TD]
[/TR]
[TR]
[TD]Name[/TD]
[TD]Owner[/TD]
[TD]Time[/TD]
[TD]Start[/TD]
[TD]End[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]10[/TD]
[/TR]
[TR]
[TD]Test6[/TD]
[TD]Resurs 7[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Test7[/TD]
[TD]Resurs 8[/TD]
[TD="align: right"]88[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody><colgroup><col><col><col><col span="2"><col span="10"></colgroup>[/TABLE]

Thanks in advance!
Robert
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try:
Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    Dim foundRng As Range
    Dim foundNum As Range
    For Each rng In Sheets("Sheet1").Range("A2:A" & LastRow)
        Set foundRng = Sheets("Sheet2").Range("A:A").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
        If Not foundRng Is Nothing Then
            Set foundNum = Sheets("Sheet2").Rows(1).Find(Sheets("Sheet1").Cells(1, "F"))
            Sheets("Sheet2").Cells(foundRng.Row, foundNum.Column) = Sheets("Sheet1").Cells(rng.Row, "F")
            Set foundNum = Sheets("Sheet2").Rows(1).Find(Sheets("Sheet1").Cells(1, "G"))
            Sheets("Sheet2").Cells(foundRng.Row, foundNum.Column) = Sheets("Sheet1").Cells(rng.Row, "G")
            Set foundNum = Sheets("Sheet2").Rows(1).Find(Sheets("Sheet1").Cells(1, "H"))
            Sheets("Sheet2").Cells(foundRng.Row, foundNum.Column) = Sheets("Sheet1").Cells(rng.Row, "H")
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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