Speeding up VBA Code that Sets Pivot Table Filters











up vote
2
down vote

favorite












I've written out some VBA code that sets various filters onto pivottables. The problem I am experiencing is that it is extremely slow and bogs down my system heavily.



I've tried implementing various things to speed up the code, but nothing really has that much of an effect.



The reason I am doing it this way is the filters need to be dynamic and I am using multiple data sources, so just using a single slicer doesn't work in my case.



Here is my code:



 Private Sub Worksheet_Calculate()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
ActiveSheet.DisplayPageBreaks = False


Dim DivRef, RegRef, DistRef, ZoneRef As String
DivRef = Sheet5.Range("AH6").Value
RegRef = Sheet5.Range("AH7").Value
DistRef = Sheet5.Range("AH8").Value
ZoneRef = Sheet5.Range("AN4").Value

With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


With Sheet5.PivotTables("PivotTable9")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub


In reality, there are a number of pivottables that are being filtered, but for the sake of this post, I am only posting PivotTable21 and PivotTable9's filters, though it is all more or less the same code.



I've been told that part of the problem might be that I am using calculated fields in my pivottables, and that may bog down some of the code as well.



Does anyone have any tips for speeding this code up?










share|improve this question














bumped to the homepage by Community 33 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • You need to dim each variable. Dim DivRef, RegRef, DistRef, ZoneRef As String only ZoneRef is String the rest are Variant`.
    – IvenBach
    May 8 at 20:51










  • I may have done this wrong, so let me know... DivRef, RegRef, etc will refer to a String, like "West" or "East", etc. I'm guessing I can remove the .value at the end of the reference, though I'm not sure if this will improve speed at all. Should I still set them to Variant?
    – Darren
    May 8 at 20:54















up vote
2
down vote

favorite












I've written out some VBA code that sets various filters onto pivottables. The problem I am experiencing is that it is extremely slow and bogs down my system heavily.



I've tried implementing various things to speed up the code, but nothing really has that much of an effect.



The reason I am doing it this way is the filters need to be dynamic and I am using multiple data sources, so just using a single slicer doesn't work in my case.



Here is my code:



 Private Sub Worksheet_Calculate()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
ActiveSheet.DisplayPageBreaks = False


Dim DivRef, RegRef, DistRef, ZoneRef As String
DivRef = Sheet5.Range("AH6").Value
RegRef = Sheet5.Range("AH7").Value
DistRef = Sheet5.Range("AH8").Value
ZoneRef = Sheet5.Range("AN4").Value

With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


With Sheet5.PivotTables("PivotTable9")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub


In reality, there are a number of pivottables that are being filtered, but for the sake of this post, I am only posting PivotTable21 and PivotTable9's filters, though it is all more or less the same code.



I've been told that part of the problem might be that I am using calculated fields in my pivottables, and that may bog down some of the code as well.



Does anyone have any tips for speeding this code up?










share|improve this question














bumped to the homepage by Community 33 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • You need to dim each variable. Dim DivRef, RegRef, DistRef, ZoneRef As String only ZoneRef is String the rest are Variant`.
    – IvenBach
    May 8 at 20:51










  • I may have done this wrong, so let me know... DivRef, RegRef, etc will refer to a String, like "West" or "East", etc. I'm guessing I can remove the .value at the end of the reference, though I'm not sure if this will improve speed at all. Should I still set them to Variant?
    – Darren
    May 8 at 20:54













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I've written out some VBA code that sets various filters onto pivottables. The problem I am experiencing is that it is extremely slow and bogs down my system heavily.



I've tried implementing various things to speed up the code, but nothing really has that much of an effect.



The reason I am doing it this way is the filters need to be dynamic and I am using multiple data sources, so just using a single slicer doesn't work in my case.



Here is my code:



 Private Sub Worksheet_Calculate()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
ActiveSheet.DisplayPageBreaks = False


Dim DivRef, RegRef, DistRef, ZoneRef As String
DivRef = Sheet5.Range("AH6").Value
RegRef = Sheet5.Range("AH7").Value
DistRef = Sheet5.Range("AH8").Value
ZoneRef = Sheet5.Range("AN4").Value

With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


With Sheet5.PivotTables("PivotTable9")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub


In reality, there are a number of pivottables that are being filtered, but for the sake of this post, I am only posting PivotTable21 and PivotTable9's filters, though it is all more or less the same code.



I've been told that part of the problem might be that I am using calculated fields in my pivottables, and that may bog down some of the code as well.



Does anyone have any tips for speeding this code up?










share|improve this question













I've written out some VBA code that sets various filters onto pivottables. The problem I am experiencing is that it is extremely slow and bogs down my system heavily.



I've tried implementing various things to speed up the code, but nothing really has that much of an effect.



The reason I am doing it this way is the filters need to be dynamic and I am using multiple data sources, so just using a single slicer doesn't work in my case.



Here is my code:



 Private Sub Worksheet_Calculate()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
ActiveSheet.DisplayPageBreaks = False


Dim DivRef, RegRef, DistRef, ZoneRef As String
DivRef = Sheet5.Range("AH6").Value
RegRef = Sheet5.Range("AH7").Value
DistRef = Sheet5.Range("AH8").Value
ZoneRef = Sheet5.Range("AN4").Value

With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


With Sheet5.PivotTables("PivotTable9")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.PivotFields("Region2").CurrentPage = RegRef
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub


In reality, there are a number of pivottables that are being filtered, but for the sake of this post, I am only posting PivotTable21 and PivotTable9's filters, though it is all more or less the same code.



I've been told that part of the problem might be that I am using calculated fields in my pivottables, and that may bog down some of the code as well.



Does anyone have any tips for speeding this code up?







vba excel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 8 at 16:52









Darren

111




111





bumped to the homepage by Community 33 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 33 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • You need to dim each variable. Dim DivRef, RegRef, DistRef, ZoneRef As String only ZoneRef is String the rest are Variant`.
    – IvenBach
    May 8 at 20:51










  • I may have done this wrong, so let me know... DivRef, RegRef, etc will refer to a String, like "West" or "East", etc. I'm guessing I can remove the .value at the end of the reference, though I'm not sure if this will improve speed at all. Should I still set them to Variant?
    – Darren
    May 8 at 20:54


















  • You need to dim each variable. Dim DivRef, RegRef, DistRef, ZoneRef As String only ZoneRef is String the rest are Variant`.
    – IvenBach
    May 8 at 20:51










  • I may have done this wrong, so let me know... DivRef, RegRef, etc will refer to a String, like "West" or "East", etc. I'm guessing I can remove the .value at the end of the reference, though I'm not sure if this will improve speed at all. Should I still set them to Variant?
    – Darren
    May 8 at 20:54
















You need to dim each variable. Dim DivRef, RegRef, DistRef, ZoneRef As String only ZoneRef is String the rest are Variant`.
– IvenBach
May 8 at 20:51




You need to dim each variable. Dim DivRef, RegRef, DistRef, ZoneRef As String only ZoneRef is String the rest are Variant`.
– IvenBach
May 8 at 20:51












I may have done this wrong, so let me know... DivRef, RegRef, etc will refer to a String, like "West" or "East", etc. I'm guessing I can remove the .value at the end of the reference, though I'm not sure if this will improve speed at all. Should I still set them to Variant?
– Darren
May 8 at 20:54




I may have done this wrong, so let me know... DivRef, RegRef, etc will refer to a String, like "West" or "East", etc. I'm guessing I can remove the .value at the end of the reference, though I'm not sure if this will improve speed at all. Should I still set them to Variant?
– Darren
May 8 at 20:54










2 Answers
2






active

oldest

votes

















up vote
0
down vote













I find that when you set PivotTable.ManualUpdate = True, Excel seems to often set that back to False for many of the changes you can make to PivotTables. It does depend on the change, but I think changing PivotFields is one of those things that triggers this.



So you could try going to every PivotField change and resetting ManualUpdate to True after it. Such as this:



With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.ManualUpdate = True
.PivotFields("Region2").CurrentPage = RegRef
.ManualUpdate = True
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


I know it seems silly, but I've seen this work.






share|improve this answer





















  • Hmmm interesting. I wouldn't have thought of this. I will give it a try and see if it speeds things up a bit. Thanks!
    – Darren
    May 8 at 21:33










  • You could test this theory by doing Debug.Print to the Immediate windows and seeing if the ManualUpdate for the PivotTable in question is still set to True. Prior to updating it everywhere.
    – PerryJ
    May 8 at 22:22


















up vote
0
down vote













I think a problem you might have is that you have this in Worksheet_Calculate and you set your Application.Calculation to automatic -



Private Sub Worksheet_Calculate()
...
Application.Calculation = xlCalculationAutomatic
End Sub


You are applying all of these filters every time anything calculates on whatever sheet this is on. Do you need that? I doubt it - maybe set your sheet to manual calculation or move the event out of the calculate event and set up a button or something.





When you declare variables, you need to give them all a type -



Dim DivRef, RegRef, DistRef, ZoneRef As String


This declares DivRef, RegRef and DistRef as Variant and only ZoneRef as string. Variants can slow down your code, but I doubt that's really the problem here. It's good practice, however, to always type them. You need to type each one e.g.



Dim divRef as String, regRef as String, distRef as String, zoneRef as String


Or more appropriately-



Dim division As String
Dim region As String
Dim district As String


I don't see you using zone so I didn't include it.



I also changed the naming, you want your names to be clear and concise.






share|improve this answer





















  • Really appreciate the comment! To answer your question, yes, I am applying these filters every time something calculates on the sheet. I'm using excel 2010 without PowerPivot. The issue is that I have 5-6 different data sources and I need all the data sources to be controlled by 1 slicer. Anytime a slicer item is clicked, RegRef, DistRef, etc, will change based on the slicer selection and the VBA filters will filter the pivot table, even if it's not connected to the original slicer.
    – Darren
    May 9 at 17:45











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193932%2fspeeding-up-vba-code-that-sets-pivot-table-filters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













I find that when you set PivotTable.ManualUpdate = True, Excel seems to often set that back to False for many of the changes you can make to PivotTables. It does depend on the change, but I think changing PivotFields is one of those things that triggers this.



So you could try going to every PivotField change and resetting ManualUpdate to True after it. Such as this:



With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.ManualUpdate = True
.PivotFields("Region2").CurrentPage = RegRef
.ManualUpdate = True
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


I know it seems silly, but I've seen this work.






share|improve this answer





















  • Hmmm interesting. I wouldn't have thought of this. I will give it a try and see if it speeds things up a bit. Thanks!
    – Darren
    May 8 at 21:33










  • You could test this theory by doing Debug.Print to the Immediate windows and seeing if the ManualUpdate for the PivotTable in question is still set to True. Prior to updating it everywhere.
    – PerryJ
    May 8 at 22:22















up vote
0
down vote













I find that when you set PivotTable.ManualUpdate = True, Excel seems to often set that back to False for many of the changes you can make to PivotTables. It does depend on the change, but I think changing PivotFields is one of those things that triggers this.



So you could try going to every PivotField change and resetting ManualUpdate to True after it. Such as this:



With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.ManualUpdate = True
.PivotFields("Region2").CurrentPage = RegRef
.ManualUpdate = True
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


I know it seems silly, but I've seen this work.






share|improve this answer





















  • Hmmm interesting. I wouldn't have thought of this. I will give it a try and see if it speeds things up a bit. Thanks!
    – Darren
    May 8 at 21:33










  • You could test this theory by doing Debug.Print to the Immediate windows and seeing if the ManualUpdate for the PivotTable in question is still set to True. Prior to updating it everywhere.
    – PerryJ
    May 8 at 22:22













up vote
0
down vote










up vote
0
down vote









I find that when you set PivotTable.ManualUpdate = True, Excel seems to often set that back to False for many of the changes you can make to PivotTables. It does depend on the change, but I think changing PivotFields is one of those things that triggers this.



So you could try going to every PivotField change and resetting ManualUpdate to True after it. Such as this:



With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.ManualUpdate = True
.PivotFields("Region2").CurrentPage = RegRef
.ManualUpdate = True
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


I know it seems silly, but I've seen this work.






share|improve this answer












I find that when you set PivotTable.ManualUpdate = True, Excel seems to often set that back to False for many of the changes you can make to PivotTables. It does depend on the change, but I think changing PivotFields is one of those things that triggers this.



So you could try going to every PivotField change and resetting ManualUpdate to True after it. Such as this:



With Sheet5.PivotTables("PivotTable21")
.ManualUpdate = True
.PivotFields("Division2").CurrentPage = DivRef
.ManualUpdate = True
.PivotFields("Region2").CurrentPage = RegRef
.ManualUpdate = True
.PivotFields("District2").CurrentPage = DistRef
.ManualUpdate = False
End With


I know it seems silly, but I've seen this work.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 8 at 21:27









PerryJ

711




711












  • Hmmm interesting. I wouldn't have thought of this. I will give it a try and see if it speeds things up a bit. Thanks!
    – Darren
    May 8 at 21:33










  • You could test this theory by doing Debug.Print to the Immediate windows and seeing if the ManualUpdate for the PivotTable in question is still set to True. Prior to updating it everywhere.
    – PerryJ
    May 8 at 22:22


















  • Hmmm interesting. I wouldn't have thought of this. I will give it a try and see if it speeds things up a bit. Thanks!
    – Darren
    May 8 at 21:33










  • You could test this theory by doing Debug.Print to the Immediate windows and seeing if the ManualUpdate for the PivotTable in question is still set to True. Prior to updating it everywhere.
    – PerryJ
    May 8 at 22:22
















Hmmm interesting. I wouldn't have thought of this. I will give it a try and see if it speeds things up a bit. Thanks!
– Darren
May 8 at 21:33




Hmmm interesting. I wouldn't have thought of this. I will give it a try and see if it speeds things up a bit. Thanks!
– Darren
May 8 at 21:33












You could test this theory by doing Debug.Print to the Immediate windows and seeing if the ManualUpdate for the PivotTable in question is still set to True. Prior to updating it everywhere.
– PerryJ
May 8 at 22:22




You could test this theory by doing Debug.Print to the Immediate windows and seeing if the ManualUpdate for the PivotTable in question is still set to True. Prior to updating it everywhere.
– PerryJ
May 8 at 22:22












up vote
0
down vote













I think a problem you might have is that you have this in Worksheet_Calculate and you set your Application.Calculation to automatic -



Private Sub Worksheet_Calculate()
...
Application.Calculation = xlCalculationAutomatic
End Sub


You are applying all of these filters every time anything calculates on whatever sheet this is on. Do you need that? I doubt it - maybe set your sheet to manual calculation or move the event out of the calculate event and set up a button or something.





When you declare variables, you need to give them all a type -



Dim DivRef, RegRef, DistRef, ZoneRef As String


This declares DivRef, RegRef and DistRef as Variant and only ZoneRef as string. Variants can slow down your code, but I doubt that's really the problem here. It's good practice, however, to always type them. You need to type each one e.g.



Dim divRef as String, regRef as String, distRef as String, zoneRef as String


Or more appropriately-



Dim division As String
Dim region As String
Dim district As String


I don't see you using zone so I didn't include it.



I also changed the naming, you want your names to be clear and concise.






share|improve this answer





















  • Really appreciate the comment! To answer your question, yes, I am applying these filters every time something calculates on the sheet. I'm using excel 2010 without PowerPivot. The issue is that I have 5-6 different data sources and I need all the data sources to be controlled by 1 slicer. Anytime a slicer item is clicked, RegRef, DistRef, etc, will change based on the slicer selection and the VBA filters will filter the pivot table, even if it's not connected to the original slicer.
    – Darren
    May 9 at 17:45















up vote
0
down vote













I think a problem you might have is that you have this in Worksheet_Calculate and you set your Application.Calculation to automatic -



Private Sub Worksheet_Calculate()
...
Application.Calculation = xlCalculationAutomatic
End Sub


You are applying all of these filters every time anything calculates on whatever sheet this is on. Do you need that? I doubt it - maybe set your sheet to manual calculation or move the event out of the calculate event and set up a button or something.





When you declare variables, you need to give them all a type -



Dim DivRef, RegRef, DistRef, ZoneRef As String


This declares DivRef, RegRef and DistRef as Variant and only ZoneRef as string. Variants can slow down your code, but I doubt that's really the problem here. It's good practice, however, to always type them. You need to type each one e.g.



Dim divRef as String, regRef as String, distRef as String, zoneRef as String


Or more appropriately-



Dim division As String
Dim region As String
Dim district As String


I don't see you using zone so I didn't include it.



I also changed the naming, you want your names to be clear and concise.






share|improve this answer





















  • Really appreciate the comment! To answer your question, yes, I am applying these filters every time something calculates on the sheet. I'm using excel 2010 without PowerPivot. The issue is that I have 5-6 different data sources and I need all the data sources to be controlled by 1 slicer. Anytime a slicer item is clicked, RegRef, DistRef, etc, will change based on the slicer selection and the VBA filters will filter the pivot table, even if it's not connected to the original slicer.
    – Darren
    May 9 at 17:45













up vote
0
down vote










up vote
0
down vote









I think a problem you might have is that you have this in Worksheet_Calculate and you set your Application.Calculation to automatic -



Private Sub Worksheet_Calculate()
...
Application.Calculation = xlCalculationAutomatic
End Sub


You are applying all of these filters every time anything calculates on whatever sheet this is on. Do you need that? I doubt it - maybe set your sheet to manual calculation or move the event out of the calculate event and set up a button or something.





When you declare variables, you need to give them all a type -



Dim DivRef, RegRef, DistRef, ZoneRef As String


This declares DivRef, RegRef and DistRef as Variant and only ZoneRef as string. Variants can slow down your code, but I doubt that's really the problem here. It's good practice, however, to always type them. You need to type each one e.g.



Dim divRef as String, regRef as String, distRef as String, zoneRef as String


Or more appropriately-



Dim division As String
Dim region As String
Dim district As String


I don't see you using zone so I didn't include it.



I also changed the naming, you want your names to be clear and concise.






share|improve this answer












I think a problem you might have is that you have this in Worksheet_Calculate and you set your Application.Calculation to automatic -



Private Sub Worksheet_Calculate()
...
Application.Calculation = xlCalculationAutomatic
End Sub


You are applying all of these filters every time anything calculates on whatever sheet this is on. Do you need that? I doubt it - maybe set your sheet to manual calculation or move the event out of the calculate event and set up a button or something.





When you declare variables, you need to give them all a type -



Dim DivRef, RegRef, DistRef, ZoneRef As String


This declares DivRef, RegRef and DistRef as Variant and only ZoneRef as string. Variants can slow down your code, but I doubt that's really the problem here. It's good practice, however, to always type them. You need to type each one e.g.



Dim divRef as String, regRef as String, distRef as String, zoneRef as String


Or more appropriately-



Dim division As String
Dim region As String
Dim district As String


I don't see you using zone so I didn't include it.



I also changed the naming, you want your names to be clear and concise.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 9 at 0:32









Raystafarian

5,7941048




5,7941048












  • Really appreciate the comment! To answer your question, yes, I am applying these filters every time something calculates on the sheet. I'm using excel 2010 without PowerPivot. The issue is that I have 5-6 different data sources and I need all the data sources to be controlled by 1 slicer. Anytime a slicer item is clicked, RegRef, DistRef, etc, will change based on the slicer selection and the VBA filters will filter the pivot table, even if it's not connected to the original slicer.
    – Darren
    May 9 at 17:45


















  • Really appreciate the comment! To answer your question, yes, I am applying these filters every time something calculates on the sheet. I'm using excel 2010 without PowerPivot. The issue is that I have 5-6 different data sources and I need all the data sources to be controlled by 1 slicer. Anytime a slicer item is clicked, RegRef, DistRef, etc, will change based on the slicer selection and the VBA filters will filter the pivot table, even if it's not connected to the original slicer.
    – Darren
    May 9 at 17:45
















Really appreciate the comment! To answer your question, yes, I am applying these filters every time something calculates on the sheet. I'm using excel 2010 without PowerPivot. The issue is that I have 5-6 different data sources and I need all the data sources to be controlled by 1 slicer. Anytime a slicer item is clicked, RegRef, DistRef, etc, will change based on the slicer selection and the VBA filters will filter the pivot table, even if it's not connected to the original slicer.
– Darren
May 9 at 17:45




Really appreciate the comment! To answer your question, yes, I am applying these filters every time something calculates on the sheet. I'm using excel 2010 without PowerPivot. The issue is that I have 5-6 different data sources and I need all the data sources to be controlled by 1 slicer. Anytime a slicer item is clicked, RegRef, DistRef, etc, will change based on the slicer selection and the VBA filters will filter the pivot table, even if it's not connected to the original slicer.
– Darren
May 9 at 17:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193932%2fspeeding-up-vba-code-that-sets-pivot-table-filters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin