Data filter and copy paste the visible cells into different sheet

harinsh

Active Member
Joined
Feb 7, 2012
Messages
273
Hi,

I have data in two different sheets and one common column values...that is part# ...I have list of part numbers in sheet1 and based on each part number I have to filter the value in second sheet and copy the values (entire rows) and paste into different sheet that is sheet3. Plz note that while filtering I may get 3 or 4 rows depends on the line items. Any number rows it should get copy and paste into different sheet.

Kindly help me to get the macro code for the above requirement.

Thank you
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,

I have data in two different sheets and one common column values...that is part# ...I have list of part numbers in sheet1 and based on each part number I have to filter the value in second sheet and copy the values (entire rows) and paste into different sheet that is sheet3. Plz note that while filtering I may get 3 or 4 rows depends on the line items. Any number rows it should get copy and paste into different sheet.

Kindly help me to get the macro code for the above requirement.

Thank you

You forgot to include the location of your data on the worksheets, but here is a generic macro and you can figure out where to make the changes to fit your needs. This macro assumes that part numbers are in column A on Sheets 1 and 2. It also assumes row 1 on each sheet as header row.
Code:
Sub filtCpy()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, lr As Long, lr2 As Long, rng As Range, c As Range
Set sh1 = Sheets(1) 'Edit sheet name
Set sh2 = Sheets(2) 'Edit sheet name
Set sh3 = Sheets(3) 'Edit sheet name
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row 'Set the limit on criteria range on sheet 1, Column A
lr2 = sh2.Cells(Rows.Count, 1).End(xlUp).Row 'set the limit on search range on sheet 2, Column A
Set rng = sh1.Range("A2:A" & lr) 'Range with criteria
    For Each c In rng
        sh2.Range("A1:A" & lr).AutoFilter 1, c.Value, VisibleDropDown:=xlNo 'Filter search range
        On Error Resume Next 'Avoid interruption when no items filtered.
        'copy filtered items to sheet 3
        sh2.Range("A2:A" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Copy sh3.Cells(Rows.Count, 1).End(xlUp)(2)
        On Error GoTo 0 'Restore error function
        sh2.Range("A1:A" & lr).AutoFilter 'turn off autofilter
    Next
End Sub
The comments in the code tell you what it is doing so you can make any necessary changes
 
Upvote 0
Thanks for your code...its working fine however in sheet 2 its copying only one line item.. refer the below example and out put which I got from the above macro

Sheet1
Part#
1
2
4

<colgroup><col style="width:48pt" width="64"> </colgroup><tbody>
</tbody>

Sheet 2
Part#NameQty Price
2sdf1 50.00
4asf2 20.00
35sdf3 30.00
65sdgc4 53.00
256sd5 45.00
32er6 68.00
45cgdfg9 98.00
2eress7 45.00
4s10 12.00

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>


OUtput
2sdf1 50.00
4asf2 20.00

<colgroup><col span="4"></colgroup><tbody>
</tbody>

Desire output...
2sdf1 50.00
2eress7 45.00
4asf2 20.00
4s10 12.00

<colgroup><col span="4"></colgroup><tbody>
</tbody>

Thank you....
 
Upvote 0
hi
try this code
Excel 2007
A
1Part#
21
32
44

<colgroup><col style="width: 25pxpx"><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



======


Excel 2007
ABCD
1Part#NameQtyPrice
22sdf150
34asf220
435sdf330
565sdgc453
6256sd545
732er668
845cgdfg998
92eress745
104s1012

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet2




=============

and this is he result
Excel 2007
ABCD
1Part#NameQtyPrice
22sdf150
34asf220
42eress745
54s1012

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet3




Try this Code

Code:
Sub Test()Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim LR1 As Long
Dim LR2 As Long


Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set ws3 = Sheets("Sheet3")


LR1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
LR2 = ws2.Range("A" & Rows.Count).End(xlUp).Row


ws2.Range("A1:D" & LR2).AdvancedFilter xlFilterCopy, ws1.Range("A1:A" & LR1), ws3.Range("A1:D1")
End Sub
 
Upvote 0
Awesome!!! Its working perfect ....but I need to know if I want to include another filter criteria like in Sheet 2 if I filter names which is equal to "S" hence, I want only list which contains names "S" and also corresponding Part# number...is it possible to get the output???

Thank you very much....
 
Upvote 0
if you want to do that you should use wildcard with Criteria
and you should be careful with headers
in sheet 1

Excel 2007
AB
1Part#Name
21*s*
32*s*
44*s*
Sheet1


and just edit code with this Part
ws2.Range("A1:D" & LR2).AdvancedFilter xlFilterCopy, ws1.Range("A1:A" & LR1), ws3.Range("A1:D1")
ws1.Range("A1:B" & LR!)
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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