excel vba how to filter each pivot item and copy paste to another sheet?

huiyin9218

Board Regular
Joined
Aug 7, 2018
Messages
53
Hi,

May I know how to write a code to automatically filter each component types (C type, P type) available in my data, copy paste to their respective sheet and arrange according to their component names (C-100, C-10, etc)?

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Component[/TD]
[TD]Amount[/TD]
[/TR]
[TR]
[TD]C-100[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]C-10[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]P-150[/TD]
[TD]7[/TD]
[/TR]
[TR]
[TD]C-100[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]P-100[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]C-100[/TD]
[TD]4[/TD]
[/TR]
</tbody>[/TABLE]

Table above is a example of my data. I would like to copy all C components and its amount and paste it at sheet C. In sheet C, all C components will arrange according to their component name, shown in table below

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Component Name[/TD]
[TD]Amount[/TD]
[TD][/TD]
[TD]Component Name[/TD]
[TD]Amount[/TD]
[/TR]
[TR]
[TD]C-100[/TD]
[TD]4[/TD]
[TD][/TD]
[TD]C-10[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]C-100[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]C-100[/TD]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Component P will be arrange like component C but it has to be in sheet P

I would be so grateful for your help.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
maybe something like this?

Code:
Sub selectiontest1()

Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables(1)

pt.PivotFields("Component").PivotFilters. _
        Add Type:=xlCaptionContains, Value1:="C"
pt.DataBodyRange.Cells(pt.DataBodyRange.Rows.Count, _
    pt.DataBodyRange.Columns.Count).ShowDetail = True

pt.PivotFields("Component"). _
        ClearLabelFilters

pt.PivotFields("Component").PivotFilters. _
        Add Type:=xlCaptionContains, Value1:="P"
pt.DataBodyRange.Cells(pt.DataBodyRange.Rows.Count, _
    pt.DataBodyRange.Columns.Count).ShowDetail = True

pt.PivotFields("Component"). _
        ClearLabelFilters

End Sub

the C sheet looks like this

Code:
Component	Amount
C-10          	5
C-100        	4
C-100        	4
C-100       	3

i couldnt get the separate components to display left to right like you wanted
 
Upvote 0
maybe something like this?

Code:
Sub selectiontest1()

Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables(1)

pt.PivotFields("Component").PivotFilters. _
        Add Type:=xlCaptionContains, Value1:="C"
pt.DataBodyRange.Cells(pt.DataBodyRange.Rows.Count, _
    pt.DataBodyRange.Columns.Count).ShowDetail = True

pt.PivotFields("Component"). _
        ClearLabelFilters

pt.PivotFields("Component").PivotFilters. _
        Add Type:=xlCaptionContains, Value1:="P"
pt.DataBodyRange.Cells(pt.DataBodyRange.Rows.Count, _
    pt.DataBodyRange.Columns.Count).ShowDetail = True

pt.PivotFields("Component"). _
        ClearLabelFilters

End Sub

the C sheet looks like this

Code:
Component    Amount
C-10              5
C-100            4
C-100            4
C-100           3

i couldnt get the separate components to display left to right like you wanted


Its okay. Your way is much easier. Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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