2017-05-31 131 views
0

我有一個數據透視表設置(請參閱圖像以獲取更好的想法)。我試圖從基於國家的數據透視表中提取數據。您可以看到行國家允許訪問行區域,並且行區域可以訪問行產品。我想在約束條件下複製數據:France以獲得區域,所有產品以及價格總和並將其移至sheet2。從數據透視表中提取數據

以下是我的想法,我可以如何解決這個問題,但是select方法在當前屬性/方法中不受支持。我可以怎樣去選擇法國境內的所有數據?

Sheets("MY Pivot").PivotTables("MY_Pivot").PivotFields("Country").PivotItems("France" _ 
    ).ShowDetail = True 'Show pivot item 

Sheets("MY Pivot").PivotTables("MY_Pivot").PivotFields("Country").PivotItems("France" _ 
    ).Select 'Select pivot item to copy 

Selection.Copy 'Copy the pivot items 
Sheets("Sheet2").Range("A1").Select 'Select sheet cell for paste 
Sheets("Sheet2").Paste 'Paste the selected France results 

Pivot Table

回答

1

您可以複製.PivotItems("France").DataRange.EntireRow爲 「Sheet2的」,而無需使用Select

代碼

Option Explicit 

Sub CopyPivotItemDataRange() 

Dim PvtTbl As PivotTable 
Dim PvtFld As PivotField 

' set the Pivot Table object 
Set PvtTbl = Sheets("MY Pivot").PivotTables("MY_Pivot") 
' set the Pivot Field object 
Set PvtFld = PvtTbl.PivotFields("Country") 

With PvtFld 
    .PivotItems("France").ShowDetail = True 'Show pivot item 

    ' copy >> paste the entire Range data under "France" to "Sheet2" 
    .PivotItems("France").DataRange.EntireRow.Copy Destination:=Sheets("Sheet2").Range("A1") 
End With 

End Sub