• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Not able to extract pivot table range on double click without ‘Refresh’

ThrottleWorks

Excel Ninja
I have one macro file. In this file I have sheet named as ‘Piovt_Samples’.

There are 15 different pivot tables saved in this worksheet.
This worksheet act as a reference sheet for me.

For each pivot table I need to derive some data from a different file, process the data and pass it as range of the particular pivot table.

For example, in ‘Piovt_Samples’ worksheet, Pivot Table 1, data is derived from ‘Yamaha’ file, processed, Copy required data, go to ‘Output file’ add a worksheet, paste the data here, assign this data as range to Pivot Table 1.
Close ‘Yamaha’ file without saving.

Now go to ‘Piovt_Samples’ file, copy pivot table 1, paste it in ‘Output File’.
Save ‘Output file’.

This way, I process and assign range for 15 different pivots

Now my problem is, when I re-open ‘Output file’ once macro is done and double click on any of the pivot table.

The pivot table does not get expand, I need to refresh the file before doing it.


Is there any way to avoid re-refreshing the entire file manually.

Can anyone please help me in this.

I get below mentioned message when double clicked on pivot table.
The PivotTable report was saved without the underlying data. Use the Refresh Data command to updated the report.

Code:
Set TempRng = ActiveSheet.Range("A1").CurrentRegion
                TempRng.SpecialCells(xlCellTypeVisible).Copy
              
                TempBook.Activate
                Worksheets.Add
                ActiveSheet.Paste
              
                'Enter in Pivot Table Name
                PivotName = "PivotTable1"
                'Set range for pivot table
                Set PvtRng = ActiveSheet.Range("A1").CurrentRegion
              
'Change range of pivot table
                ThisWorkbook.Activate
              
Worksheets("All_Pivots").PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
                PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PvtRng, Version:=xlPivotTableVersion10)
              
                'Ensure Pivot Table is Refreshed
                AllPvtSht.PivotTables(PivotName).RefreshTable
                AllPvtSht.Select
              
              
TempLr = TempBook.Worksheets(“YoYo”).Cells(TempBook.Worksheets(“YoYo”).Rows.Count, 1).End(xlUp).Row + 2
              
                'Copy Pivot Table Header
                AllPvtSht.Range("A6").Copy
              
                TempBook.Worksheets(“YoYo”).Cells(TempLr, 1).PasteSpecial xlPasteValues
                TempBook.Worksheets(“YoYo”).Cells(TempLr, 1).PasteSpecial xlPasteFormats
              
                Range("S2").Select
                ActiveSheet.PivotTables(“PivotTable1”).PivotSelect "", xlDataAndLabel, True
                Selection.Copy
              
                TempLr = TempBook.Worksheets(“YoYo”).Cells(TempBook.Worksheets(“YoYo”).Rows.Count, 1).End(xlUp).Row + 3
                TempBook.Worksheets(“YoYo”).Cells(TempLr, 1).PasteSpecial
                          
              

                    TempBook.Activate
                  
                    ActiveWorkbook.RefreshAll
                    TempBook.Save
 
Hi Sachin ,

In the absence of a workbook , I can only guess.

There are 2 pivot table options labelled :

Save source data with file

Refresh data when opening the file

What is the setting of these 2 options ?

Narayan
 
Hi @NARAYANK991 sir, thanks for the help.

Sorry I did not understand "What is the setting of these 2 options ?".

I tried below.
Saved the source data file. Opened final report, clicked on refresh, double click on pivot table, it works.

Save source data file, save final report, close final report, delete source data file, open final report, unable to refresh now.

The confusing part is when I do it manually with sample workbook, it works.

Book 1, create dummy data
book 2 create pivot based on data from book 1
save book 2
close book 2
open (new) book 3
paste original pivot from book 2 in book 3.
close book2
save book 3
close book 2
delete book 1 and 2
Open book 3
Double click on pivot without refresh it works.
 
The original file which I have as a reference have all these 15 pivot tables in it.
The source data of these pivot table is not available on my machine. I doubt it is even saved on user's machine also.

Even then, when I clicked on pivot table without 'Refresh' pivot table extracts range.
 
Is your PivotTable data source dynamic? (I.E. named range, table etc). This will cause issue as PivotTable will need the source to be open for dynamic ranges from another workbook.
 
Hi @Chihiro sir, thanks for the help. I am using below mentioned code. Could you please review this if possible.

Code:
Set PvtRng = DummyBook.Worksheets("sheet1").Range("A1").CurrentRegion
'Change pivot table range
ThisWorkbook.Activate

 Worksheets("All_Pivots").PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _

 PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PvtRng, Version:=xlPivotTableVersion10)

DummyBook.Save
AllPvtSht.PivotTables(PivotName).RefreshTable
AllPvtSht.Select

'Copy First Pivot in Final Report
Range("I2").Select
ActiveSheet.PivotTables("PivotTable1").PivotSelect "", xlDataAndLabel, True
Selection.Copy

TempBook.Worksheets("YoYo").Range("A7").PasteSpecial
 
I also tried changing SourceData:=PvtRng, from to SourceData:=DummyBook.Worksheets("sheet1").Range("A1").CurrentRegion

But still facing same issue.
 
I don't see anything that jumps out as issue in the code itself.

I'd definitely check the source data range and see if it's named table or range. If it is, change it to simple Range using "Convert to Range" and/or Name manager.

If you can upload a sample, I can try and run more diagnostic on it.
 
Hi @Chihiro sir, thanks a lot for the help. "I don't see anything that jumps out as issue in the code itself." is an assuring line for me.

I am really sorry, uploading is barred. Have a nice day ahead. :)
 
Your process description is confusing. Does following describe your current process correctly?

Source File --> Copy Data to Pivot Sample --> Generate Pivot --> Copy only Pivot to Output file.

If yes, then which of the info is not available to you later?
SourceData in PivotSample sheet used to build Pivot
Source File
 
Hi @shrivallabha , sorry for making it confusing.

I have saved Pivot tables as templates in my macro file itself. There are 15 of it.
Open file 1, process it, this will be the range of 1st pivot table in macro file.

Go to macro file, change the range of 1st pivot table based on file 1.
Close file 1, no need to save it.

Now open output file
Copy 1st pivot table from macro file
paste this in output file.

Repeat for all 15 pivots.

My problem is, I want to extract the data if I double click on any of the pivot table in Output file. This is not happening.

Sorry, I do not know the exact term, but when we double click last cell of pivot table it generates a sheet, do not know what we call it exactly.

This particular sheet is getting opened.
I saved pivot range in dummy file at the same location as Output file.
But does not seem feasible option for me.

If the final Output Report is sent on e-mail then saving dummy data in another file at another location won't work.
 
My doubt is, the way I am assigning range to pivot table is incorrect.

This method of mine requires data source file to be saved at an accessible path to Output Report pivot tables.

If the source file is deleted then pivot table won’t get refreshed.
But this problem does not occur when we manually create any pivot table.

Even if we do not save the source data, pivot table still extract source data from it’s own memory.
I am looking at changing the method of assigning range to pivot table and check if it works.

Thanks.
 
If we save only Output Report on our machine and double click on pivot table, source sheets gets opened. No need to save another 2 files. I am trying to achieve this. But not able to do so.
 

Attachments

  • MacroFile.xlsx
    12.1 KB · Views: 1
  • OutputReport.xlsx
    12 KB · Views: 2
  • SourceData.xls
    30 KB · Views: 1
Ok figured out the issue.

You need to set .SaveData property of PivotTables in Output file to True or it won't save the pivotcache.

So something like...

Code:
With TempBook.Worksheets("YoYo")
    For Each pvttable In .PivotTables
        pvttable.SaveData = True
    Next
End With

TempBook.Close SaveChanges:=True
 
Back
Top