VBA - 如何从数据透视表中过滤和复制数据?

时间:2017-06-02 09:05:38

标签: excel vba excel-vba

美好的一天!假设我的excel中有这个Pivot表。从下图中,我想过滤 BrandA 并复制所有品牌 BrandA 并将其粘贴到新工作表中,同样适用于 BrandX BrandZ的即可。 enter image description here

请帮帮我:(

谢谢!

已编辑1 以下是我录制的宏。

Sub MacroTest2()

With ActiveSheet.PivotTables("PivotTable3").PivotFields("Brand")
    .PivotItems("BrandA").Visible = False
    .PivotItems("BrandZ").Visible = False
End With

Range("A4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy

Sheets.Add After:=ActiveSheet
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Pivot").Select

ActiveSheet.PivotTables("PivotTable3").PivotFields("Brand").ClearAllFilters
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Brand")
    .PivotItems("BrandX").Visible = False
    .PivotItems("BrandZ").Visible = False
End With

Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy

Sheets.Add After:=ActiveSheet
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Pivot").Select

ActiveSheet.PivotTables("PivotTable3").PivotFields("Brand").ClearAllFilters
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Brand")
    .PivotItems("BrandA").Visible = False
    .PivotItems("BrandX").Visible = False
End With

Range("A4:A5").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Pivot").Select
End Sub

0 个答案:

没有答案