macro create pivot table

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Macro to create a Pivot Table

Macro to create a Pivot Table

resolvedResolved · High Priority · Version 2016

Chris has attended:
Excel Intermediate course

Macro to create a Pivot Table

Hi, I'm trying to record a macro to create a pivot table in a worksheet. The worksheet is one where the pivot table will be updated on a weekly basis so therefor the first step of my recording is to delete the previous weeks pivot table in order create the current weeks pivot table in the same cells as other sheets in the workbook will vlookup the information in those cells for updated calculations.

The issue I'm having is that when I run the macro I get an error message (run time error 1004) stating that the changes cant be made for a pivot table in the selected cells.

This is the vba log for the macro, the highlighted error in the thread is this line:

"Selection.Clear"

Any help would be most appreciated, thank you.


' Keyboard Shortcut: Ctrl+i
'
Range("Q1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Clear
Range("Q3").Select
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Range("Table3").Select
Range("D4").Activate
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table3", Version:=6).CreatePivotTable TableDestination:= _
"SHEET NAME", TableName:="PivotTable4", DefaultVersion _
:=6
Sheets("SHEET NAME").Select
Cells(3, 17).Select
With ActiveSheet.PivotTables("PivotTable4")
.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("PivotTable4").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable4").RepeatAllLabels xlRepeatLabels
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable4").PivotFields("NAME")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable4").PivotFields("NAME")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable4").PivotFields("NAME").CurrentPage _
= "(All)"
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Absence Type")
.PivotItems("0").Visible = False
.PivotItems(" NAME").Visible = False
End With
ActiveSheet.PivotTables("PivotTable4").PivotFields("NAME"). _
EnableMultiplePageItems = True
ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
"PivotTable4").PivotFields("NAME"), "NAME", xlSum
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 11
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub


RE: Macro to create a Pivot Table

Hi Chris,

Thank you for the forum question.

PivotTable VBA is complicated and something we have on our Excel Advanced VBA course.

Recording PivotTable macros can be done but a lot of things are happen behind the scene in Excel when we create PivotTable and a lot of things can go wrong. Excel create something called a Pivot Cache which are a storage in the computer's memory. The storage in the memory also need to be cleared.

I prefer to have the PivotTabe in a new sheet every time I need to update my report. So my code I use for PivotTables delete the old sheet with the old PivotTable, add a new sheet, and create the new PivotTable on the new sheet.

This clear the Pivot cache and avoid many of the issues you can get with PivotTable VBA.


Please let know if you have anything else in the sheet where you want to create the PivotTable

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

See also:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Recovering Unsaved Work in Excel 2010

Ever closed your Excel workbook in a hurry without saving your work beforehand? Here's how to get it back.

Choose File then Info, click Manage Versions and then Recover Unsaved Workbooks which will automatically find the spreadsheets that haven't been saved.

View all Excel hints and tips


Server loaded in 0.11 secs.