Multi-select Listbox to filter pivot table runs slow

boazstuff

Board Regular
Joined
Jan 15, 2009
Messages
59
Hi all,

I have a multi select listbox which I used to filter pivot table pagefields. (method 1)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
I also have a code to filter the pivot table and update the selection on the list box. (method 2)<o:p></o:p>
The code works perfectly fine except that it is very slow using method 2.<o:p></o:p>
<o:p></o:p>
I understand it is due to a lot of loopings but I am not sure how I can improve it…<o:p></o:p>
<o:p></o:p>
Appreciate if someone can enlighten and help, thanks.

Regards,
Boaz<o:p></o:p>
<o:p></o:p>
Private Sub ListBox1_Change()<o:p></o:p>
<o:p></o:p>
Dim rngSelection As Range<o:p></o:p>
Dim lngItem As Long<o:p></o:p>
<o:p></o:p>
Set rngSelection = Worksheets("Control").Range("MultiListOutPut")<o:p></o:p>
rngSelection.Value = 0<o:p></o:p>
<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
<o:p></o:p>
For lngItem = 1 To ListBox1.ListCount<o:p></o:p>
<o:p></o:p>
If ListBox1.Selected(lngItem - 1) Then<o:p></o:p>
If rngSelection(lngItem).Value = 1 Then<o:p></o:p>
rngSelection(lngItem).Value = 0<o:p></o:p>
Else<o:p></o:p>
rngSelection(lngItem).Value = 1<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
Next lngItem<o:p></o:p>
<o:p></o:p>
Application.ScreenUpdating = True<o:p></o:p>
<o:p></o:p>
Call Pivot<o:p></o:p>
<o:p></o:p>
End Sub

<o:p></o:p>
<o:p></o:p>
Sub Pivot()<o:p></o:p>
<o:p></o:p>
Dim PTGWP As PivotTable<o:p></o:p>
Set PTGWP = Sheets("Pivot").PivotTables("PTGWP")<o:p></o:p>
<o:p></o:p>
Application.Calculation = xlCalculationManual<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
<o:p></o:p>
PTGWP.PivotFields("Product Line").CurrentPage = "(All)"<o:p></o:p>
With PTGWP.PivotFields("Product Line")<o:p></o:p>
.PivotItems("BD").Visible = Range("BD")<o:p></o:p>
.PivotItems("CL").Visible = Range("L")<o:p></o:p>
.PivotItems("CM").Visible = Range("CM")<o:p></o:p>
.PivotItems("CP").Visible = Range("CP")<o:p></o:p>
.PivotItems("CO").Visible = Range("CO")<o:p></o:p>
.PivotItems("CE").Visible = Range("CE")<o:p></o:p>
.PivotItems("EC").Visible = Range("EC")<o:p></o:p>
.PivotItems("EN").Visible = Range("EN")<o:p></o:p>
.PivotItems("GH").Visible = Range("GH")<o:p></o:p>
.PivotItems("IH").Visible = Range("IH")<o:p></o:p>
.PivotItems("IP").Visible = Range("IP")<o:p></o:p>
.PivotItems("MC").Visible = Range("MC")<o:p></o:p>
.PivotItems("MH").Visible = Range("MH")<o:p></o:p>
.PivotItems("PA").Visible = Range("PA")<o:p></o:p>
.PivotItems("PL").Visible = Range("PL")<o:p></o:p>
.PivotItems("PM").Visible = Range("PM")<o:p></o:p>
.PivotItems("PP").Visible = Range("PP")<o:p></o:p>
.PivotItems("PR").Visible = Range("PR")<o:p></o:p>
.PivotItems("SL").Visible = Range("SL")<o:p></o:p>
.PivotItems("SP").Visible = Range("SP")<o:p></o:p>
.PivotItems("TR").Visible = Range("TR")<o:p></o:p>
End With<o:p></o:p>

<o:p></o:p>
Application.Calculation = xlCalculationAutomatic<o:p></o:p>
Application.ScreenUpdating = True<o:p></o:p>
<o:p></o:p>
Set PTGWP = Nothing<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

boazstuff

Board Regular
Joined
Jan 15, 2009
Messages
59
Very sorry, i forgot to paste another code which is the code that runs slow. When this code is fired, it will run through a long loops with the Listbox1_change macro. And that is the reason for the slowness... :confused:


Sub ListBox1_lines()
Dim rngSelection As Range
Set rngSelection = Worksheets("Control").Range("MultiListOutPut")
rngSelection.Value = 0
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
ListBox1.Selected(0) = False
ListBox1.Selected(1) = False
ListBox1.Selected(2) = True
ListBox1.Selected(3) = False
ListBox1.Selected(4) = False
ListBox1.Selected(5) = False
ListBox1.Selected(6) = False
ListBox1.Selected(7) = False
ListBox1.Selected(8) = True
ListBox1.Selected(9) = True
ListBox1.Selected(10) = False
ListBox1.Selected(11) = False
ListBox1.Selected(12) = False
ListBox1.Selected(13) = True
ListBox1.Selected(14) = True
ListBox1.Selected(15) = True
ListBox1.Selected(16) = True
ListBox1.Selected(17) = False
ListBox1.Selected(18) = False
ListBox1.Selected(19) = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 

Forum statistics

Threads
1,141,284
Messages
5,705,510
Members
421,399
Latest member
hjweiss00

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
Top