Find a match and paste rows to a new sheet

nike

Board Regular
Joined
Feb 12, 2008
Messages
113
Office Version
  1. 365
Platform
  1. Windows
Hi,


I need help on creating a macro to compare column A in 2 different sheets, and if there's a match to create another worksheet to paste all the rows that matched column A. Thank you in advance!

I just put the comma to distinguish the columns for the example below.


EXAMPLE:


Sheet1
COLUMN A
1
ORANGE
CAT




SHEET2
A, B, C, D
1, MAPLE, STREET, CALIFORNA
1, MAPLE, STREET, CALIFORNA
ORANGE, APPLE, FRUIT, HIGHWAY
ORANGE, VEGGIE, GREEN, HIGHWAY
CAT, DOG, PET, ADOPT



SHEET3 (MATCHED COMBOS) OUTPUT
A, B, C, D
1, MAPLE, STREET, CALIFORNA
1, MAPLE, STREET, CALIFORNA
ORANGE, APPLE, FRUIT, HIGHWAY
ORANGE, VEGGIE, GREEN, HIGHWAY
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
See if this works for you.

Code:
Sub makeMatchLixt()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, c As Range, fn As Range, Adr As String
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("sheet2")
Sheets.Add After:=ThisWorkbook.Sheets(Sheets.Count)
Set sh3 = ActiveSheet
    For Each c In sh1.Range("A1", sh1.Cells(Rows.Count, 1).End(xlUp)) 'Assumes no header.  If header, change to "A2"
        Set fn = sh2.Range("A:A").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                Adr = fn.Address
                Do
                    fn.EntireRow.Copy sh3.Cells(Rows.Count, 1).End(xlUp)(2)
                    Set fn = sh2.Range("A:A").FindNext(fn)
                Loop While fn.Address <> Adr
            End If
        Set fn = Nothing
    Next
End Sub
 
Upvote 0
Hi JLGWhiz,

It works! Thank you so much! I just have one issue, the output starts on A2, even when i changed it to A2 for the header. If it's A2 it doesn't include the header and it starts on A2, but when i change it to A1, it includes the header but also starts in A2. It's not a big deal, i can just delete the top row, but if you can fix it, i'd appreciate it. Thanks again!
 
Upvote 0
Hi JLGWhiz,

It works! Thank you so much! I just have one issue, the output starts on A2, even when i changed it to A2 for the header. If it's A2 it doesn't include the header and it starts on A2, but when i change it to A1, it includes the header but also starts in A2. It's not a big deal, i can just delete the top row, but if you can fix it, i'd appreciate it. Thanks again!

Code:
Sub makeMatchLixt2()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, c As Range, fn As Range, Adr As String
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("sheet2")
Sheets.Add After:=ThisWorkbook.Sheets(Sheets.Count)
Set sh3 = ActiveSheet
    For Each c In sh1.Range("A1", sh1.Cells(Rows.Count, 1).End(xlUp)) 'Assumes no header.  If header, change to "A2"
        Set fn = sh2.Range("A:A").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                Adr = fn.Address
                Do
                    If sh3.Cells(1, 1) = "" Then
                        fn.EntireRow.Copy sh3.Range("A1")
                    Else
                        fn.EntireRow.Copy sh3.Cells(Rows.Count, 1).End(xlUp)(2)
                    End If
                    Set fn = sh2.Range("A:A").FindNext(fn)
                Loop While fn.Address <> Adr
            End If
        Set fn = Nothing
    Next
End Sub
 
Upvote 0
Hi JLGWhiz,

The code works great, can this code below be added to your code to be able to do vlookup right after the output, on the same sheet? Thanks!

Columns("D:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.NumberFormat = "General"
Range("D1").Select
ActiveCell.FormulaR1C1 = "STATE ID"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],PREP!C[-3]:C,4,0)"
Selection.AutoFill Destination:=Range("D2:D91")
Range("D2:D91").Select
Cells.Select
Selection.Columns.AutoFit
Range("A1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,861
Members
449,052
Latest member
Fuddy_Duddy

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