01-06-21 12:47 PM
Answered! Go to Answer.
01-06-21 06:40 PM
Hi Theodoros,
While I checked stackoverflow for the error it indicates that the error is due to incorrect name of field but according to you it works when Refresh is turned off. So maybe you can try this before changing the Criteria and the turn it back to true at the end. Not sure if that'll work
ws.PivotTables("PivotTable2").PivotCache.RefreshOnFileOpen = False
01-06-21 01:55 PM
Hi Theodoros,
What error do you get when you try to open the file with your code?
01-06-21 02:12 PM
01-06-21 06:40 PM
Hi Theodoros,
While I checked stackoverflow for the error it indicates that the error is due to incorrect name of field but according to you it works when Refresh is turned off. So maybe you can try this before changing the Criteria and the turn it back to true at the end. Not sure if that'll work
ws.PivotTables("PivotTable2").PivotCache.RefreshOnFileOpen = False
02-06-21 10:14 AM
02-06-21 12:02 PM
Hi Theodoros,
Are you getting error while opening workbook or while applying filter? I tried changing filter of sample excel with two pivot and it worked for me with "Refresh data when opening the file" checked. I added ws.PivotTables(PivotTableName).PivotCache.Refresh
at the very end of the code in 'Add Criteria to Page Field Filter for Pivot Table' code to refresh table after the criteria change.
My steps
1. Create Instance
2. Open Workbook
3. Add Criteria to Page Field Filter for Pivot Table
4. Close All Instance (with save)
I would recommend you record a macro from the Developer tab of Excel to check if the field name and everything is all right.
Thanks,
02-06-21 01:41 PM
02-06-21 01:53 PM
02-06-21 05:06 PM
04-06-21 01:52 PM
Hi Theodoros,
If you are okay with adding libraries you can try this, I was working on creating a VBO with ClosedXML, I added an Update Pivot Criteria action which you can try.
OpenWorkBook ->Update Pivot Criteria->Close Workbook
Thanks,