您可以在VBA中更改数据透视表的源代码吗?

时间:2019-02-14 19:41:07

标签: excel vba pivot-table

为我每周运行的报告记录了一个宏,其中包含一个数据透视表。麻烦的是,每周范围都不一样。我尝试只为列设置参数,但是由于数据下方的所有空白单元格,我在数据透视表中得到了一个空白列。有没有一种方法可以更改SourceData:= _“ UBS Pre-Orders Report!R1C1:R1048576C19”(

 Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "UBS Pre-Orders Report!R1C1:R1048576C19", Version:=6).CreatePivotTable _
        TableDestination:="Sheet4!R3C1", TableName:="PivotTable2", DefaultVersion _
        :=6
    Sheets("Sheet4").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable2")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("PivotTable2").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Item Number")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
        "PivotTable2").PivotFields("Quantity On Order"), "Sum of Quantity On Order", _
        xlSum
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Ship Date")
        .Orientation = xlColumnField
        .Position = 1
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

我在这里使用了这篇文章,它把我带到了我需要去的地方,这实际上是将范围设置为一个表,然后使用Table作为源代码 https://chandoo.org/wp/tables-pivottables-and-macros-music-to-your-ears/