Conditional copy from one worksheet to another, based on values in key columns

svendfj

Board Regular
Joined
Mar 19, 2011
Messages
99
Office Version
  1. 2016
Platform
  1. Windows
Hello,

My scouring of previous posts somehow has not turned up something close to what I am looking for. Thanks in advance for any guidance you can please lend.

A simplified version: I have a 'Master' worksheet that has IDs listed in column A, starting at row 2. In 'Sheet2' of the same workbook, column G has a subset of the same IDs, and then columns H, I and J have 'Color', 'Taste' and 'Smell', respectively. I want to:
(a) loop through the IDs in Column A in 'Master', and
(b) for each ID, I want to loop through Column G of 'Sheet2', and
(c) when there is a match, I want to copy the information on that row - in Columns H, I and J - and copy that information into Columns B, C and D of the row with that ID in 'Master'.

Thank you for any leads! Sven
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hope this helps.
Code:
Sub test()
 Dim rng As Range, c, cnt As Long
 Dim Mws As Worksheet, ws2 As Worksheet
 Dim r As Long
 
 Set Mws = Sheets("Master")
 Set ws2 = Sheets("Sheet2")
 cnt = 2
 With Mws
    Set rng = ws2.Range(ws2.Range("G2"), ws2.cells(Rows.count, 7).End(xlUp))
    For Each c In .Range(.Range("A2"), .cells(Rows.count, 1).End(xlUp))
        r = 0
        On Error Resume Next
        r = WorksheetFunction.match(c, rng, 0)
        On Error GoTo 0
        If r <> 0 Then
            .Range(.cells(cnt, 2), .cells(cnt, 4)).Value = ws2.Range(ws2.cells(r + 1, 8), ws2.cells(r + 1, 10)).Value
        End If
        cnt = cnt + 1
    Next
 End With
 End Sub
 
Upvote 0
no loop

Code:
 Sub test()
 Dim LR As Long
 With Sheets("Master")
    LR = .cells(Rows.count, 1).End(xlUp).Row
    .Range(.Range("B2"), .cells(LR, 4)).FormulaR1C1 = "=VLOOKUP(RC1,Sheet2!C7:C10,COLUMN(),0)"
    .Range(.Range("B2"), .cells(LR, 4)).Value = .Range(.Range("B2"), .cells(LR, 4)).Value
 End With
 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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