如何为数据透视表选择数据源的动态范围(从活动选择中)

时间:2018-07-22 17:33:07

标签: excel vba excel-vba

帮我从选择中获取我的数据透视表的源数据

Sheets("Filtered Data").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

我的选择过程到此结束,数据透视表选择数据,您能帮助我使其动态吗?

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Filtered Data!R1C1:R21C8", Version:=xlPivotTableVersion15).CreatePivotTable _
    TableDestination:="Snap!R6C6", TableName:="PivotTable13", DefaultVersion _
    :=xlPivotTableVersion15

格式化数据透视表在那里没有那么重要

Sheets("Snap").Select
Cells(6, 6).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable13").PivotFields("CMP")
    .Orientation = xlRowField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable13").PivotFields("Area")
    .Orientation = xlColumnField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable13").AddDataField ActiveSheet.PivotTables( _
    "PivotTable13").PivotFields("Site Name"), "Count of Site Name", xlCount
ActiveWorkbook.ShowPivotTableFieldList = False
Columns("F:K").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

1 个答案:

答案 0 :(得分:0)

尝试类似的东西:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:= Sheets("Filtered Data").Range("A1").CurrentRegion, _
    Version:=xlPivotTableVersion15).CreatePivotTable _
    TableDestination:="Snap!R6C6", TableName:="PivotTable13", DefaultVersion _
    :=xlPivotTableVersion15