writing macro refresh multiple
RH

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Writing Macro to Refresh multiple pivot tables with a button

Writing Macro to Refresh multiple pivot tables with a button

resolvedResolved · High Priority · Version 2016

Kay has attended:
Excel Advanced course
Excel Advanced - Formulas & Functions course

Writing Macro to Refresh multiple pivot tables with a button

I have a query regarding Refresh multiple pivot tables with a button with Macro function

I have selected the range under Formula > Name manager > added in range of my workbook for which I want to refresh the pivots.

I have written 2 types of Macros separately as below

1. Sub RefreshAllPivotTables()

ThisWorkbook.RefreshAll

End Sub

2. Sheets (“Sheet1”).Select
ActiveSheet.PivotTables (“PivotTable1”).RefreshTable
Sheets (“Sheet2”).Select
ActiveSheet.PivotTables (“PivotTable2””).RefreshTable

Both scenarios do not appear to be working for me.

If you provide any help or guidance on this it would be very much appreciated.

RE: Writing Macro to Refresh multiple pivot tables with a button

Hi Kay,

Thank you for the forum question.

I cannot understand why ThisWorkbook.RefreshAll does not work for you.

Does the Refresh All button on the Data tab works???

Is the code in the same workbook as the PivotTables??

Is the source data in the same workbook as the PivotTables??



Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best 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

RE: Writing Macro to Refresh multiple pivot tables with a button

Hi Jen

Thanks for your prompt reply

I have assigned the macro in the same workbook.

The source data is in the same workbook as the Pivot Tables

I did get manage to “refresh all” button, clicking on “assign macro”, selecting “refresh” and hitting ok. However this lead to a further problem that when you click the “refresh all” button, you get “run time error ‘1004’. I have tried but can’t find a fix for this.

The pivots still do not refresh to reflect what is in the source data

I have sent you an example of the workbook to have a look at

Thanks for taking the time to help
Kay

RE: Writing Macro to Refresh multiple pivot tables with a button

Hi Kay,

We have not received any files from you. Can I ask you to send it to jens@stl-training.co.uk


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best 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

RE: Writing Macro to Refresh multiple pivot tables with a button

Hi Jen

Thanks for your prompt reply.


I have sent my file to your email address

Please can you check and confirm you have received it

Many thanks

Kay

RE: Writing Macro to Refresh multiple pivot tables with a button

Hi Kay,

I have received the file, but there is no data or PivotTables or macros in the workbook. I cannot find out what the problem is without having this in the workbook.

Run Time error 1004 can be a lot of things. You have an error in your workbook otherwise the code would have worked and you wouldn't have got the error message.

You ask how Pivot data is selected using VBA. Pivot data are stored in cache in your computer's memory. Every time you open the workbook Excel will create a "space" in your computer's memory for the pivot data.

The code below is an example how to create PivotTable from VBA. I have added some explanations inside the code:

Sub MakePivotTable()

Dim pt As PivotTable 'declare pivot table object as declaring variable
Dim PvtCache As PivotCache 'holds the data from the source. pivot cache=data
Dim pf As PivotField
Dim pi As PivotItem 'each cell content the data under each pivot field

On Error Resume Next
Sheets("Pivot").Select
ActiveSheet.PivotTables("MyPT").TableRange2.Clear 'deletes the Pivot table

'set the cache of pt

Sheets("data").Select
Set PvtCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Range("Data")) 'Data is a range name
Sheets("Pivot").Select
Set pt = ActiveSheet.PivotTables.Add(PvtCache, Range("a1"), "MyPT")
With pt
'''''add fields
.PivotFields("Date").Orientation = xlRowField
'.PivotFields("Name").Orientation = xlColumnField
.PivotFields("Product").Orientation = xlRowField
.PivotFields("Price").Orientation = xlDataField 'values
'set the number format
.DataBodyRange.NumberFormat = "£ #,##0.00"
'classic pivot table view
.RowAxisLayout xlTabularRow
'add calculated field
'.CalculatedFields.Add "Commission", "=Price*.05"
'.PivotFields("Commission").Orientation = xlDataField

'add name to page field
.PivotFields("Name").Orientation = xlPageField

End With


Set pf = pt.PivotFields("Name")
With pf
For Each pi In pf.PivotItems
If pi.Name = "James" Or pi.Name = "Bob" Then
pi.Visible = True
Else
pi.Visible = False
End If
Next pi
End With

Set pf = pt.PivotFields("Product")
With pf
For Each pi In pf.PivotItems
If pi.Name = "Ball" Then
pi.Visible = True
Else
pi.Visible = False
End If
Next pi
End With


End Sub
Sub ChangeToPercent()
With ActiveSheet.PivotTables("MyPT")
.DataFields(1).Calculation = xlPercentOfColumn
.DataBodyRange.NumberFormat = "0.00%"
End With


End Sub
Sub ChangeToCount()


With ActiveSheet.PivotTables("MyPT")
.DataFields(1).Calculation = xlNormal
.DataFields(1).Function = xlCount
.DataBodyRange.NumberFormat = "0"

End With
End Sub





Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best 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

RE: Writing Macro to Refresh multiple pivot tables with a button

Hi Jen

Thanks for taking the time to help with this however this is very complex to what I am looking for as I am not selecting pivot data using VBA

I am looking to automatically refresh Pivot Tables in an Excel workbook which has the tabs I have highlighted in my file

All my source data and pivots are in the same excel workbook

I have written the macro as:

Sub RefreshAllPivotTables()

ThisWorkbook.RefreshAll

End Sub

Ideally I don’t want to manually refresh my Pivot tables after refreshing the data worksheet as I add more information to this each month.

My questions would be

How do you set the data source range to automatically read all the additional data that has been added

How do you assign the macro to the button so that it knows to refresh all my pivot tables all at once

Many thanks
Kay

RE: Writing Macro to Refresh multiple pivot tables with a button

Hi Kay,

Sorry I have misunderstand what you want to do.

You do not need to refresh the data you need to change the source range, when you add new data to the source list.

Refresh will only update the PivotTables if you change the data in the range you already have the reference to in the PivotTable, if you want to add more new data to the source range you will need to update the range.

You can automate this, but then you will need to format your source data as table. Click inside your source data and click Table in the Tables group on the Insert tab. Click OK. Now you you can add as many new records to the source and refresh and then you PivotTables will update.

I will send you an example to your email.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best 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

Fri 18 Aug 2017: Automatically marked as resolved.


 

Excel tip:

Customize the toolbar in Excel 2010

You can create your own toolbar which contains your favourite or most used tools. This will make using Excel much more efficient. To do this, you need to click on View, then select Customize Quick access Toolbars and then select Customize. A list of tools will then appear on the screen of which you can add or remove them as you please.

View all Excel hints and tips


Server loaded in 0.05 secs.