Copy and paste multiple matching rows to a new sheet

Multim

New Member
Joined
Apr 17, 2024
Messages
4
Office Version
  1. 2021
Hi Newbie to VBA here

I have a list of part numbers in a sheet called Review in column A (with a header line in A1)

This list can be from 20 to 100+ part numbers long.

These part numbers will match a master list with multiple entries in a sheet called Master column C (with a header line in row 1).

I would like a VBA code to search all of the part numbers from the sheet called Review from A2 down to the last part number and copy and paste all matching rows from the sheet called Master into a new sheet including the header line in row 1.

I have search but can not find a way to do this, can anyone help please.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Can you post a small sample of both sets of data? please use the xl2bb tool if you can (link in editing bar, and in the signature box below). If you cannot use that, then post your data in a table (copy and paste cells from your worksheets). Do not post an image as the forum needs to completely recreate your scenario that way.

Are you open to VBA solutions?

PS Welcome to the forum.
 
Upvote 0
Hi awoohaw

VBA would be preferred please

Samples copied & pasted below

Master Worksheet
GRN NoLine NoPart NoDescriptionGroup CodeCategoryLocationQty LeftUOMUOPUnit Of ConvStatus IDStatusblnShowInDropdownLifed Item
10922​
1​
RP010001SR-30 SOLUBLE RELEASE SUPPORT CANISTER FORTUS PLUS 0355-03110 - 4503D PRINTINGG5A
1​
EAEA
1​
1​
Live
TRUE​
FALSE​
10676​
1​
RP010001SR-30 SOLUBLE RELEASE SUPPORT CANISTER FORTUS PLUS 0355-03110 - 4503D PRINTINGG5A
5​
EAEA
1​
1​
Live
TRUE​
FALSE​
10676​
2​
RP010002ABS-M30 FILAMENT CANISTER FORTUS PLUS NATURAL 0355-021103D PRINTINGG6A
6​
EAEA
1​
1​
Live
TRUE​
FALSE​
7307​
1​
RP010002ABS-M30 FILAMENT CANISTER FORTUS PLUS NATURAL 0355-021103D PRINTINGG6A
2​
EAEA
1​
1​
Live
TRUE​
FALSE​
1003​
1​
RP010003ABS-M30 FILAMENT CANISTER FORTUS PLUS BLACK 0355-021123D PRINTINGG7A
1​
EAEA
1​
1​
Live
TRUE​
FALSE​
1004​
1​
RP010004SR-30 SUPPORT CARTRIDGE (FORTUS 250MC) 0340-305003D PRINTINGG8A
5​
EAEA
1​
1​
Live
TRUE​
FALSE​
1005​
1​
RP010005P430 DIMENSION ABS PLUS MATERIAL CASSETTE WHITE 0340-212013D PRINTINGG9A
8​
EAEA
1​
1​
Live
TRUE​
FALSE​
4994​
5​
RP010006FOUNDATION SHEET SMALL FORTUS 450/900MC 0325-002753D PRINTINGG7B
60​
EAPK
20​
1​
Live
TRUE​
FALSE​
1007​
1​
RP010007ULTEM 1010 MODEL FORTUS 450MC NATURAL 0355-023303D PRINTINGG10A
3​
EAEA
1​
1​
Live
TRUE​
FALSE​
1008​
1​
RP010008ULTEM 1010 SUPPORT FORTUS 450MC NATURAL 0355-032403D PRINTINGG11A
4​
EAEA
1​
1​
Live
TRUE​
FALSE​
1009​
1​
RP010011ABS-M30I FILAMENT CANISTER FORTUS PLUS NATURAL 0355-021203D PRINTINGG12A
3​
EAEA
1​
1​
Live
TRUE​
FALSE​
6606​
1​
AB010001ABRANET DISC 150MM P80ABRASIVESE3A
1​
boxbox
1​
1​
Live
TRUE​
FALSE​
4273​
3​
AB010001ABRANET DISC 150MM P80ABRASIVESE3A
2​
boxbox
1​
1​
Live
TRUE​
FALSE​

Review Worksheet
Part No
RP010001
RP010002
RP010003
RP010004
RP010005
RP010006
RP010007
RP010008
RP010011
AB010001
AB010002
AB010003
AB010005
AB010006
AB010012
AB010013
AB010016
AB010017
 
Upvote 0
See if this does what you need:

VBA Code:
Sub CopyPartsForReview()

    Dim wb As Workbook
    Dim shtMaster As Worksheet, shtReview As Worksheet, shtOut As Worksheet
    Dim rowLastMstr As Long, rowLastRev As Long
    Dim rngMstr As Range, rngRev As Range
    Dim arrMstr As Variant, arrRev As Variant, arrOut As Variant
    Dim dictRev As Object, dictKey As String
    Dim i As Long, j As Long, rowOut As Long
    
    Set wb = ActiveWorkbook
    Set shtMaster = wb.Worksheets("Master")
    Set shtReview = wb.Worksheets("Review")
    
    With shtMaster
        rowLastMstr = .Range("C" & Rows.Count).End(xlUp).Row
        Set rngMstr = .Range("A1:O" & rowLastMstr)
        arrMstr = rngMstr.Value2
    End With
    
    ReDim arrOut(1 To UBound(arrMstr, 1), 1 To UBound(arrMstr, 2))
    
    With shtReview
        rowLastRev = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngRev = .Range("A2:A" & rowLastRev)
        arrRev = rngRev.Value2
    End With
    
    Set dictRev = CreateObject("Scripting.dictionary")
    dictRev.CompareMode = vbTextCompare
    
    ' Review Parts into Dictionary
    For i = 1 To UBound(arrRev)
        dictKey = arrRev(i, 1)
        If Not dictRev.exists(dictKey) Then
            dictRev(dictKey) = i
        End If
    Next i

    For i = 2 To UBound(arrMstr)
        dictKey = arrMstr(i, 3)
        If dictRev.exists(dictKey) Then
            rowOut = rowOut + 1
            For j = 1 To UBound(arrMstr, 2)
                arrOut(rowOut, j) = arrMstr(i, j)
            Next j
        End If
    Next i
    
    If rowOut > 0 Then
        With wb
            Set shtOut = .Sheets.Add(After:=.Worksheets(.Worksheets.Count))
        End With
        
        With shtOut
            rngMstr.Rows(1).Copy Destination:=.Range("A1")
            .Range("A2").Resize(rowOut, UBound(arrOut, 2)).Value = arrOut
            .Columns("A").Resize(, UBound(arrOut, 2)).AutoFit
        End With
    End If
    
End Sub
 
Upvote 1
Solution
Thank you Alex

This works perfectly

If I wanted to extend the copy range to column "x" can i modify this line like this - Set rngMstr = .Range("A1:X" & rowLastMstr) ?
 
Upvote 0
See if this does what you need:

VBA Code:
Sub CopyPartsForReview()

    Dim wb As Workbook
    Dim shtMaster As Worksheet, shtReview As Worksheet, shtOut As Worksheet
    Dim rowLastMstr As Long, rowLastRev As Long
    Dim rngMstr As Range, rngRev As Range
    Dim arrMstr As Variant, arrRev As Variant, arrOut As Variant
    Dim dictRev As Object, dictKey As String
    Dim i As Long, j As Long, rowOut As Long
   
    Set wb = ActiveWorkbook
    Set shtMaster = wb.Worksheets("Master")
    Set shtReview = wb.Worksheets("Review")
   
    With shtMaster
        rowLastMstr = .Range("C" & Rows.Count).End(xlUp).Row
        Set rngMstr = .Range("A1:O" & rowLastMstr)
        arrMstr = rngMstr.Value2
    End With
   
    ReDim arrOut(1 To UBound(arrMstr, 1), 1 To UBound(arrMstr, 2))
   
    With shtReview
        rowLastRev = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngRev = .Range("A2:A" & rowLastRev)
        arrRev = rngRev.Value2
    End With
   
    Set dictRev = CreateObject("Scripting.dictionary")
    dictRev.CompareMode = vbTextCompare
   
    ' Review Parts into Dictionary
    For i = 1 To UBound(arrRev)
        dictKey = arrRev(i, 1)
        If Not dictRev.exists(dictKey) Then
            dictRev(dictKey) = i
        End If
    Next i

    For i = 2 To UBound(arrMstr)
        dictKey = arrMstr(i, 3)
        If dictRev.exists(dictKey) Then
            rowOut = rowOut + 1
            For j = 1 To UBound(arrMstr, 2)
                arrOut(rowOut, j) = arrMstr(i, j)
            Next j
        End If
    Next i
   
    If rowOut > 0 Then
        With wb
            Set shtOut = .Sheets.Add(After:=.Worksheets(.Worksheets.Count))
        End With
       
        With shtOut
            rngMstr.Rows(1).Copy Destination:=.Range("A1")
            .Range("A2").Resize(rowOut, UBound(arrOut, 2)).Value = arrOut
            .Columns("A").Resize(, UBound(arrOut, 2)).AutoFit
        End With
    End If
   
End Sub
Dear Alex, could I ask you if I need to matching from multi sheet such as master, master1, master2, master3 sheets. How would you vba code change?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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