Jan 15 2021 07:28 PM - edited Jan 15 2021 07:43 PM
I'm using Excel in Office 365 for Windows (lastest version).
One of my systems generates an Excel notebook with a few hundred tabs, about 50 of which contain pivot tables. All the pivot tables are collapsed.
If I save that notebook without manually expanding each pivot table in each tab, all the data hidden within them is lost.
Is there a way to automatically expand all the pivot tables, in all tabs, in a notebook?
Can this be done within Excel itself, or does it require a macro?
I did find this, from 2014, which claims to *collapse* all pivot tables in a notebook:
https://www.mrexcel.com/board/threads/collapse-all-pivot-tables-in-workbook.828962/
Jan 16 2021 09:51 AM
Here is another example - unfortunately in Deutach (you have to translate it where you need it), maybe it will help your projects.
Maybe that will bring you a little closer to your goal.
https://www.luis.uni-hannover.de/fileadmin/kurse/material/VBA/Excel2007/excel_PivotTable.pdf
Hope I could help you at least a little.
I would be happy to know if I could help.
I wish you continued success with Excel
Nikolino
I know I don't know anything (Socrates)
Jan 16 2021 05:32 PM
@NikolinoDE Thanks for trying, but I'm afraid that I don't speak German, and a Google translation isn't typically sufficient for technical instructions.
Jan 16 2021 05:46 PM
Hi @byte99
Just wanted to check what you mean by this "If I save that notebook without manually expanding each pivot table in each tab, all the data hidden within them is lost. "
Sounds unusual
Jan 16 2021 07:22 PM - edited Jan 17 2021 11:18 PM
If I expand the pivot tables so that I can see the underlying data, and then save the workbook, all the underlying data is saved within the workbook. But if I leave the pivot table as-is (collapsed), save the workbook, and then try to expand the pivot table, I get this error message:
If I use the Refresh Data command, I get this message:
But if I then click OK, it tells me it can't link to the source file.
This is a known issue where I work. Our IT dept. is aware of it. I suspect the problem is that, when the files are generated, they are generated with links to the underlying data, rather than the underlying data itself. When you open the pivot table, it imports that data. But if you save before doing this, the link is broken, and it is no longer possible to import the data. Thus our SOP is to first manually expand all the pivot tables, and then save.
It might be related to this:
https://www.pivot-table.com/2015/01/28/saving-source-data-with-pivot-table-file/
But, understably, I don't want to go into each of the 50 tabs and change a setting for each pivot table. And even if there were one setting I could change for the entire workbook, to ensure all data is saved, I would still need to expand all the pivot tables manually anyways, for presentation purposes.
Jan 16 2021 09:48 PM
Hi @byte99
Ah ok, sounds like it will be a macro... there's another one here which you could combine with a for each Pivot routine...
https://www.excelcampus.com/vba/expand-collapse-entire-pivot-table-fields-buttons/
For Each sh In ThisWorkbook.Worksheets
'If there is any pivot table on the sheet
If sh.PivotTables.Count > 0 Then
'Loop through all the pivots on the sheet
For Each pvt In sh.PivotTables
Call ExpandPivotFields
Next pvt
End If
Sounds like the "ThisWorkbook" element will need to be changed so you can save the routine in a separate workbook to run each time.
Jan 17 2021 03:36 PM - edited Jan 17 2021 03:59 PM
As I've never written an Excel Macro before, I'm not quite following how to extend that code to loop through all the sheets in a Workbook.
I tried the Macro you linked, and it does work. To execute it, I just need to open the Workbook containing that Macro (which I downloaded from your supplied link). Then if I open one of my Workbooks, and go to Developer->Macros, I can select the Macro I need (since the Workbook with the Macro is open, it can access its Macros), and Run. But, as you know, this works for the open sheet only.
The structure of the Macro is as follows:
**************
Option Explicit
Sub Some_Name_1( )
[Several lines of code]
End Sub
Sub Some_Name_2( )
[Several lines of code]
End Sub
***********************
So exactly where do I insert the above code into your code to create the needed loop?
[I also posted that question on the link itself, asking them how to extend their code to loop through all the sheets in a Workbook.]
Jan 17 2021 04:49 PM - edited Jan 18 2021 01:45 PM
As I've never written an Excel Macro before, I'm not quite following how to extend that code to loop through all the sheets in a Workbook.
I tried the Macro you linked, and it does work. To execute it, I just need to open the Workbook containing that Macro (which I downloaded from your supplied link). Then if I open one of my Workbooks, and go to Developer->Macros, I can select the Macro I need (since the Workbook with the Macro is open, it can access its Macros), and Run. But, as you know, this works for the open sheet only.
I see your Macro has the following statement:
Call ExpandPivotFields
But how will this call the specific Macro I need, since the link contains a package consisting of several different Macros?:
Should I substitute the actual name of the Macro I want, which is "Expand_Entire_RowField" for "ExpandPivotFields"?
Alternately, the structure of the Macro I need is:
**************
Option Explicit
Sub ExpandEntireRowField ()
[Several lines of code]
End Sub
***********************
So could I insert the above code into your code to create the needed loop? If so, how would I do that?
[I also posted that question on the link itself, asking them how to extend their code to loop through all the sheets in a Workbook.]
Jan 17 2021 10:13 PM
Jan 17 2021 10:48 PM
SolutionHi @byte99
Not my best work but something like this. You could either put this code in your personal macro workbook (and assign a short cut key, or assign it to Quick Access Toolbar button) for repeated use. Or (simpler to start with) press Alt F11 on your Pivot Table file and paste this code into the Workbook window
Sub ExpandAll()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim iFieldCount As Long
Dim iPosition As Long
Dim sh As Worksheet
Dim wkbkTarget As Workbook
Application.ScreenUpdating = False
Set wkbkTarget = ActiveWorkbook
For Each sh In wkbkTarget.Worksheets
'If there is any pivot table on the sheet
If sh.PivotTables.Count > 0 Then
sh.Activate
'Expand the lowest position field in the Rows area
'that is currently expanded (showing details)
For Each pt In sh.PivotTables
'Create reference to 1st pivot table on sheet
'Can be changed to reference a specific sheet or pivot table.
' Set pt = ActiveSheet.PivotTables(1)
'Count fields in Rows area minus 1 (last field can't be expanded)
iFieldCount = pt.RowFields.Count - 1
'Loop by position of field
For iPosition = 1 To iFieldCount
'Loop fields in Rows area
For Each pf In pt.RowFields
'If position matches first loop variable then
If pf.Position = iPosition Then
'Loop each pivot item
For Each pi In pf.PivotItems
'If pivot item is collapsed then
If pi.ShowDetail = False Then
'Expand entire field
pf.ShowDetail = True
'Exit the loop
GoTo NextPT
End If
Next pi
End If
Next pf
'If the Exit Sub line is not hit then the
'loop will continue to the next field position
Next iPosition
NextPT:
Next pt
End If
Next sh
Set wkbkTarget = Nothing
Application.ScreenUpdating = True
End Sub
Jan 18 2021 01:41 PM
Thanks Wyn--works beautifully!
Jan 17 2021 10:48 PM
SolutionHi @byte99
Not my best work but something like this. You could either put this code in your personal macro workbook (and assign a short cut key, or assign it to Quick Access Toolbar button) for repeated use. Or (simpler to start with) press Alt F11 on your Pivot Table file and paste this code into the Workbook window
Sub ExpandAll()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim iFieldCount As Long
Dim iPosition As Long
Dim sh As Worksheet
Dim wkbkTarget As Workbook
Application.ScreenUpdating = False
Set wkbkTarget = ActiveWorkbook
For Each sh In wkbkTarget.Worksheets
'If there is any pivot table on the sheet
If sh.PivotTables.Count > 0 Then
sh.Activate
'Expand the lowest position field in the Rows area
'that is currently expanded (showing details)
For Each pt In sh.PivotTables
'Create reference to 1st pivot table on sheet
'Can be changed to reference a specific sheet or pivot table.
' Set pt = ActiveSheet.PivotTables(1)
'Count fields in Rows area minus 1 (last field can't be expanded)
iFieldCount = pt.RowFields.Count - 1
'Loop by position of field
For iPosition = 1 To iFieldCount
'Loop fields in Rows area
For Each pf In pt.RowFields
'If position matches first loop variable then
If pf.Position = iPosition Then
'Loop each pivot item
For Each pi In pf.PivotItems
'If pivot item is collapsed then
If pi.ShowDetail = False Then
'Expand entire field
pf.ShowDetail = True
'Exit the loop
GoTo NextPT
End If
Next pi
End If
Next pf
'If the Exit Sub line is not hit then the
'loop will continue to the next field position
Next iPosition
NextPT:
Next pt
End If
Next sh
Set wkbkTarget = Nothing
Application.ScreenUpdating = True
End Sub