VBA:创建数据透视表

时间:2019-08-01 23:01:39

标签: excel vba

我的代码一直显示预期的表达式错误

我得到的错误来自

ActiveWorkbook.Worksheets("Sheet2").PivotTables("PivotTable16").PivotCache. _
        CreatePivotTable TableDestination:="Sheet2!E3F16", TableName:="PivotTable17", DefaultVersion:=xlPivotTableVersionCurrent

完整宏:

Sub Macro5()
'
' Macro5 Macro
'

'
    ActiveCell.Select
    ActiveWorkbook.Worksheets("Sheet2").PivotTables("PivotTable16").PivotCache. _
        CreatePivotTable TableDestination:="Sheet2!E3F16", TableName:="PivotTable17", DefaultVersion:=xlPivotTableVersionCurrent

    Sheets("Sheet2").Select
    Cells(3, 16).Select
    With ActiveSheet.PivotTables("PivotTable17")
        .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
End Sub

1 个答案:

答案 0 :(得分:0)

没有“选择和更正图纸参考”的代码:

Sub Macro5()

    ActiveWorkbook.Worksheets("Sheet2").PivotTables("PivotTable16").PivotCache. _
        CreatePivotTable TableDestination:="Sheet2!E3:F16", TableName:="PivotTable17", DefaultVersion:=xlPivotTableVersionCurrent

    With Sheets("Sheet2").PivotTables("PivotTable17")
        .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
End Sub

始终避免在VBA中使用“选择”,这只会导致问题。

还要确保在运行代码之前已删除旧的PivotTable17,否则您将面临更多问题。