attempting to speed up pivot table filter selection

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,056
Office Version
  1. 365
Platform
  1. Windows
Hi folks, i keep getting errors with the second of the two macros below while PT.ManualUpdate=True is used. I am trying to speed up the filter selection process which looks at the value selected in a combo box and if that value does not equal the pivot item value it hides the pivot item. it loops through all pivot items. without the PT.ManualUPdate bit, the second macro delivers the correct result - it just takes forever and a day...

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> ChooseMAM()<br><br>****<SPAN style="color:#00007F">Dim</SPAN> critRange <SPAN style="color:#00007F">As</SPAN> Range, c <SPAN style="color:#00007F">As</SPAN> Range, ws <SPAN style="color:#00007F">As</SPAN> Worksheet, PT <SPAN style="color:#00007F">As</SPAN> PivotTable, pi <SPAN style="color:#00007F">As</SPAN> PivotItem<br>****<SPAN style="color:#00007F">Set</SPAN> critRange = Worksheets("Annual Results").Range("C2")<br>****<SPAN style="color:#00007F">Dim</SPAN> strFields <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, strFields2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> graphSheets <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>****strFields = Worksheets("Annual Results").Range("b2").Value<br>****strFields2 = "Debtor Normal Name"<br>****PT.ManualUpdate = <SPAN style="color:#00007F">True</SPAN><br>****strValue = critRange.Value<br>****Worksheets("Annual Results").Range("D3").Value = "0"<br><br>****<SPAN style="color:#00007F">Set</SPAN> graphSheets = Sheets(Array("Rep Sales Data", "TY Sales Data"))<br><br>****<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> graphSheets<br>********<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> PT <SPAN style="color:#00007F">In</SPAN> ws.PivotTables<br>************<SPAN style="color:#00007F">With</SPAN> PT.PivotFields(strFields2)<br>****************.ClearAllFilters<br>************<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br>************<SPAN style="color:#00007F">With</SPAN> PT.PivotFields(strFields)<br>****************.ClearAllFilters<br>****************<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> pi <SPAN style="color:#00007F">In</SPAN> PT.PivotFields(strFields).PivotItems<br>********************<SPAN style="color:#00007F">If</SPAN> pi.Value = strValue <SPAN style="color:#00007F">Then</SPAN><br>************************.CurrentPage = strValue<br>************************<SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN><br>********************<SPAN style="color:#00007F">Else</SPAN><br>************************.CurrentPage = "(All)"<br>********************<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>****************<SPAN style="color:#00007F">Next</SPAN> pi<br>************<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>********<SPAN style="color:#00007F">Next</SPAN> PT<br>****<SPAN style="color:#00007F">Next</SPAN> ws<br>****PT.ManualUpdate = <SPAN style="color:#00007F">False</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> ChooseAccount()<br><br>****<SPAN style="color:#00007F">Dim</SPAN> AccountRange <SPAN style="color:#00007F">As</SPAN> Range, c <SPAN style="color:#00007F">As</SPAN> Range, ws <SPAN style="color:#00007F">As</SPAN> Worksheet, PT <SPAN style="color:#00007F">As</SPAN> PivotTable, pi <SPAN style="color:#00007F">As</SPAN> PivotItem<br>****<SPAN style="color:#00007F">Dim</SPAN> strDebtorName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, strFields2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> graphSheets <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br>****strFields2 = "Debtor Normal Name"<br>****<SPAN style="color:#00007F">Set</SPAN> AccountRange = Worksheets("Annual Results").Range("c3")<br>****strDNValue = AccountRange.Value<br>****PT.ManualUpdate = <SPAN style="color:#00007F">True</SPAN><br>****<SPAN style="color:#00007F">Set</SPAN> graphSheets = Sheets(Array("Rep Sales Data", "TY Sales Data"))<br><br>****<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> graphSheets<br>********<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> PT <SPAN style="color:#00007F">In</SPAN> ws.PivotTables<br><br>************<SPAN style="color:#00007F">With</SPAN> PT.PivotFields(strFields2)<br>****************<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> pi <SPAN style="color:#00007F">In</SPAN> PT.PivotFields(strFields2).PivotItems<br><br>********************<SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> .PivotItems.Count<br>************************<SPAN style="color:#00007F">If</SPAN> .PivotItems(j) <> strDNValue And _<br>************************** .PivotItems(j).Visible = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>****************************.PivotItems(j).Visible = <SPAN style="color:#00007F">False</SPAN><br>************************<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>********************<SPAN style="color:#00007F">Next</SPAN> j<br><br>****************<SPAN style="color:#00007F">Next</SPAN> pi<br>************<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>********<SPAN style="color:#00007F">Next</SPAN> PT<br>****<SPAN style="color:#00007F">Next</SPAN> ws<br>****PT.ManualUpdate = <SPAN style="color:#00007F">False</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

the macros are an amalgam of other macros so there should be some cleanups to do still, so any suggestions in that direction would also be appreciated.
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi ajm,

In both functions you posted, the PT.ManualUpdate statements are outside of the For Each PT block. Try moving them inside.

Rich (BB code):
For Each ws In graphSheets
    For Each PT In ws.PivotTables
        PT.ManualUpdate = True
        With PT.PivotFields(strFields2)
            For Each pi In PT.PivotFields(strFields2).PivotItems

                For j = 1 To .PivotItems.Count
                    If .PivotItems(j) <> strDNValue And _
                       .PivotItems(j).Visible = True Then
                        .PivotItems(j).Visible = False
                    End If
                Next j
            Next pi
        End With
        PT.ManualUpdate = False
    Next PT
Next ws

I'm not sure whether this will speed your code up to an acceptable level, but it should help.
If that doesn't fix things, please provide How many PivotItems are in Debtor field? How long is it taking to run?
 
Upvote 0
hi jerry, i have moved the lines as you suggested and am running it now. it doesn't appear to have made any difference to the time it takes to run. anywhere from 2 - 5 seconds for the first combo list (23 items) and 235 seconds for the second combo list (423 items). The code you supplied me with originally (http://www.mrexcel.com/forum/showthread.php?t=575835&page=2) works better than this, i just couldn't get it to clear the second validation list.

where abouts did you get to on your trip over here?
 
Upvote 0
Hi,

Try

Code:
'Start of your code
' Speeding Up VBA Code
    With Application
        .ScreenUpdating = False 'Prevent screen flickering
        .Calculation = xlCalculationManual 'Preventing calculation
        .DisplayAlerts = False 'Turn OFF alerts
        .EnableEvents = False 'Prevent All Events
    End With
 
' Start your code here
 
 
'End of your code
'Speeding Up VBA Code
    With Application
        .ScreenUpdating = True 'Prevent screen flickering
        .Calculation = xlAutomatic 'Preventing calculation
        .DisplayAlerts = True 'Turn OFF alerts
        .EnableEvents = True 'Prevent All Events
    End With

Is it faster now?


Biz
 
Upvote 0
biz, it definitely is quicker. i know there are a lot of pivot items to compare and hide (423) but it still takes 35 seconds. I am going to run with this for this afternoon and promise further enhancements, improvements, bells and whistles, etc...
 
Upvote 0
biz, it definitely is quicker. i know there are a lot of pivot items to compare and hide (423) but it still takes 35 seconds. I am going to run with this for this afternoon and promise further enhancements, improvements, bells and whistles, etc...


I am thinking using vba arrays processing maybe faster but I am not very strong with it. Loops are slower. I'm sure someone can come with better code.

Biz
 
Upvote 0
This may help as well.

<font face=Courier New>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> PT <SPAN style="color:#00007F">In</SPAN> ws.PivotTables<br>        <SPAN style="color:#007F00">' Set update to manual to avoid recomputation while making changes</SPAN><br>        PT.ManualUpdate = <SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> PT.PivotFields(strFields2)<br>            <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Pi <SPAN style="color:#00007F">In</SPAN> PT.PivotFields(strFields2).PivotItems<br><br>                <SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> .PivotItems.Count<br>                    <SPAN style="color:#00007F">If</SPAN> .PivotItems(j) <> strDNValue And _<br>                       .PivotItems(j).Visible = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>                            .PivotItems(j).Visible = <SPAN style="color:#00007F">False</SPAN><br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Next</SPAN> j<br><br>            <SPAN style="color:#00007F">Next</SPAN> Pi<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        PT.ManualUpdate = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> PT</FONT>


EDIT: Sorry, just noticed JS411's post.
 
Last edited:
Upvote 0
Hi guys,

Part of the reason that the code checks the Visible state of each PivotItem before changing it,
is that when you have a large number of PivotItems, there is a huge time savings in only changing
the state of the ones that need to be changed.

So if you have 1 item visible and you want to change to 1 different visible item,
this is pretty fast...

Rich (BB code):
For j = 1 To .PivotItems.Count
    If .PivotItems(j) <> strDNValue And _
       .PivotItems(j).Visible = True Then
        .PivotItems(j).Visible = False
    End If
Next j

This is much slower...even though in both cases you are only changing the state of 2 items.

Rich (BB code):
For j = 1 To .PivotItems.Count
    If .PivotItems(j) <> strDNValue Then
        .PivotItems(j).Visible = False
    End If
Next j

I think the reason the code I suggested in the earlier thread worked faster, is that it was always leaving only one PivotItem Visible.
In your current process, my understanding is that you are making all PivotItems visible each time you change an Account Manager.
That makes the speed comparable to the second code snippet instead of the first.

You might want to revisit that approach and have a separate "showall" macro for those cases that you want to see all Debtors.

where abouts did you get to on your trip over here?

We visited Sidney, Ayers Rock, Alice Springs and Cairns. Wonderful people and places! :)
 
Last edited:
Upvote 0
We visited Sidney, Ayers Rock, Alice Springs and Cairns. Wonderful people and places! :)

everybody forgets brisbane... boo hoo Jerry, isn't Sydney Harbour the most captivating puddle you have ever seen?! I used to sail there every saturday afternoon prior to us moving back to brisbane before the kids were born. my wife doesn't understand my pain....

I will have a crack at a "show all" macro later this afternoon. what I have at present will get me across the line and then i will have more time after presentation to tidy things up. I'll keep this thread open until i have tied this one down.
 
Upvote 0
Replace the block with this and it will be a wee bit faster.
(423 times faster in your case). ;)

Code:
    For Each ws In graphSheets
        For Each PT In ws.PivotTables
            PT.ManualUpdate = True
            With PT.PivotFields(strFields2)
               .PivotItems(strDNValue).Visible = True
                For j = 1 To .PivotItems.Count
                    If .PivotItems(j) <> strDNValue And _
                       .PivotItems(j).Visible = True Then
                        .PivotItems(j).Visible = False
                    End If
                Next j
            End With
            PT.ManualUpdate = False
        Next PT
   Next ws
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,424
Members
452,914
Latest member
echoix

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top