cancel
Showing results for 
Search instead for 
Did you mean: 

MS Excel Pivot Table Filtering

TheodorosPapaso
Level 3
Hi All,
I'm new in BluePrism and I have the following scenario. There is an excel with 2 pivot tables. I need to change the filter, apply refresh and save the excel with the updated values in order to send as an attachment via email. Object MS Excel VBO - Extended doesn't seem to work properly for that work (I understand 'Add Criteria to Page Field for Pivot Table' action is only for creation and not in an already created pivot?), therefore I ended up trying my lock with Code stage. Unsuccessfully though so far. RefreshTable seems to work fine if I go first in the excel and uncheck the option 'Refresh data when opening the file', otherwise it gives me an error while opening the workbook. The problem is that the excel is exported with 'Refresh data when opening the file' checked. 
I tried the below code but no luck so far:

Dim ws As Object
ws = GetWorksheet(Handle, Workbook, Worksheet)
ws.Activate()
ws.AutoFilterMode = False
ws.PivotTables(PivotTableName).PivotFields(Field).CurrentPage = Criteria
ws.PivotTables(PivotTableName).RefreshTables

Input Parameters:
Handle (Number) = 1
Workbook (Text) - The name of the workbook
Worksheet (Text) - The name of the worksheet
PivotTableName (Text) - The name of the pivot table
Field (Text) - The name of the field that filter applies
Criteria (Text) - The value for the filter that I want to apply

Any Ideas are most welcome! Thanks in advance!
#pivottable​​ #pivotfilter #pivotrefresh​​​

------------------------------
Theodoros Papasotiriou
------------------------------
1 BEST ANSWER

Best Answers

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


------------------------------
Gopal Bhaire
Analyst
Accenture
------------------------------

View answer in original post

11 REPLIES 11

GopalBhaire
Level 10

Hi Theodoros,

What error do you get when you try to open the file with your code?



------------------------------
Gopal Bhaire
Analyst
Accenture
------------------------------

Hi Gopal and thanks for the quick reply. I get "Internal: Could not execute code stage because exception thrown by code stage: Unable to get the PivotFields property of the Pivot Table class"

------------------------------
Theodoros Papasotiriou
------------------------------

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


------------------------------
Gopal Bhaire
Analyst
Accenture
------------------------------

Hi Gopal,

Eventually it worked with the "Excel VBO-Extended" action "Add Criteria to Page Field Filter for Pivot Table"  (code below)
Dim ws As Object
ws = GetWorksheet(Handle, Workbook, Worksheet)
ws.Activate()
ws.PivotTables(PivotTableName).PivotFields(Field).CurrentPage = Criteria

Input
Handle (Number) = 1
Workbook (Text) - The name of the workbook
Worksheet (Text) - The name of the worksheet
PivotTableName (Text) - The name of the pivot table
Field (Text) - The name of the field that filter applies
Criteria (Text) - The value for the filter that I want to apply

The problem was that within the PivotTable Options, the "Refresh data when opening the file" (under tab Data) was unchecked, therefore I needed to trigger a Refresh (PivotTable.RefreshTable) before applying the filter...

Now, I'm trying to change this value from the code ... Tried with your code above (I assume this is what it does), but before calling the function, when trying to Open Workbook in order to read the worksheet name, I get an error when "Refresh data when opening the file" is checked (it's like a deadlock)....Not sure if I can change the value without opening the workbook somehow

Thanks Gopal!
Best,
Theo

------------------------------
Theodoros Papasotiriou
------------------------------

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,



------------------------------
Gopal Bhaire
Analyst
Accenture
------------------------------

Hi Gopal,
Unfortunately I get the error much before the 'Add Criteria to Page Field Filter for Pivot Table'.
I follow the same order as yours. Error comes on Open Workbook: "HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)".... When I uncheck manually  the "Refresh data when opening the file"  it works fine. However, the excel that I will process with my solution comes with the property checked. Therefore I'm trying to apply a code to uncheck that property before calling the Open Workbook (unsuccessfully so far)
Thanks again!

------------------------------
Theodoros Papasotiriou
------------------------------

Can you put it in recover and retry with a 30 sec sleep and check?

------------------------------
Gopal Bhaire
Analyst
Accenture
------------------------------

That was a good idea but unfortunately even after 30 sec sleep it goes back to the same error every time I call Open Workbook

------------------------------
Theodoros Papasotiriou
------------------------------

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,



------------------------------
Gopal Bhaire
Analyst
Accenture
------------------------------