Matching data from two colums on separate sheets, and copying rows of matches to sheet 3

jukurit

New Member
Joined
May 31, 2011
Messages
13
I have information on two sheets. Sheets 1 and 2. I need to mach the data in sheet 1 column C with data in sheet 2 column P. If the numbers match i need the row in sheet 2 that has the matching number to be copied into sheet 3. (there are multiple numbers that match in sheet 2 and i set by sheet 1)
Suggestions please?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Code:
Sub Collect()

    Dim sh As Worksheet
    Dim rng1 As Range, rng2 As Range, cell As Range
    
    Set sh = Sheets("Sheet3")
    
    With Sheets("Sheet1")
        Set rng1 = .Range("C1", .Range("C1").End(xlDown))
    End With
    With Sheets("Sheet2")
        Set rng2 = .Range("P1", .Range("P1").End(xlDown))
    End With
    
    For Each cell In rng2
        If WorksheetFunction.CountIf(rng1, cell.Value) Then
            sh.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next

End Sub
 
Upvote 0
Of course nothing 'cause I wrote generic code. You should alter names of your sheets. If you'd write names of those two sheets and sheet to transfer data, I'd write it in code. Ha-ha :laugh:
 
Upvote 0
im abit new to these macro thisngs. haha never done them before.
so ive named
sheet 1 = Yritys
sheet 2 = Yhteyshenkilo
sheet 3 = Valmis
 
Upvote 0
Try this one. Here code searches values from rng1 in rng2.

Code:
Sub Collect()

    Dim sh As Worksheet
    Dim rng1 As Range, rng2 As Range, cell As Range
    
    Set sh = Sheets("Valmis")
    
    With Sheets("Yritys")
        Set rng1 = .Range("C1", .Range("C1").End(xlDown))
    End With
    With Sheets("Yhteyshenkilo")
        Set rng2 = .Range("P1", .Range("P1").End(xlDown))
    End With
    
    For Each cell In rng2
        If WorksheetFunction.CountIf(rng1, cell.Value) Then
            sh.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next

End Sub
 
Upvote 0
I've made a slight change to Sektor's code, try:
Rich (BB code):
Sub Collect()
    Dim sh As Worksheet
    Dim rng1 As Range, rng2 As Range, cell As Range
 
    Set sh = Sheets("Valmis")
 
    With Sheets("Yritys")
        Set rng1 = .Range("C1", .Range("C1").End(xlDown))
    End With
    With Sheets("Yhteyshenkilo")
        Set rng2 = .Range("P1", .Range("P1").End(xlDown))
    End With
 
    For Each cell In rng2
        If WorksheetFunction.CountIf(rng1, cell.Value) Then
            cell.EntireRow.Copy
            sh.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlValues
        End If
    Next
End Sub
 
Upvote 0
It is still not doing anything. i have posted the code in the macro. but nothing happens when i run it. :(
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,319
Members
452,905
Latest member
deadwings

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