0

My code has been showing expected expression error

The error that i am getting is from

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

Full macro:

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
Mikku
  • 6,538
  • 3
  • 15
  • 38

1 Answers1

0

Code without Select and Correct Sheet Reference:

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

Always Avoid Using Select in VBA, it causes only Problems.

Also Make sure Old PivotTable17 have been deleted before Running the Code, Otherwise you will face more issues.

Mikku
  • 6,538
  • 3
  • 15
  • 38