[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
ABCDEFGH
NameOwnerTimeStartEnd456
Test6Resurs 71502018-01-012018-12-31505050
Test7Resurs 8882018-01-012018-12-31283030

<tbody>
</tbody><colgroup><col span="3"><col span="2"><col span="3"></colgroup>

Sheet2
ABCDEFGHIJKLMNO
NameOwnerTimeStartEnd12345678910
Test6Resurs 71502018-01-012018-12-31
Test7Resurs 8882018-01-012018-12-31

<tbody>
</tbody><colgroup><col><col><col><col span="2"><col span="10"></colgroup>

Thanks in advance!
Robert
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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