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)?

ComponentAmount
C-1004
C-105
P-1507
C-1003
P-1002
C-1004

<tbody>
</tbody>

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

Component NameAmountComponent NameAmount
C-1004C-105
C-1003
C-1004

<tbody>
</tbody>

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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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