Hi all,
I currently have this code to control a Pivottable with Listboxes in my Userform;
The reason why I have coded it like this with a count is that there are empty field in my source data, when filtering just with the 'else' bit of the code the Empty rows remain in the pivot table next to the selected items.
What i've done is counting the selected items of the listbox and compare this with the number of items, when there equal, then the 'empty' items need to be shown, when there not equal, I only wan't to see the selected items. (so emptyItems.visible = false)
This works, while there is something 'weird' in the last bit, while it works at the time, I dont like the code that much, in a PivotTable it isnt possible to view nothing, so i've put the "Next pi" at the bottom. While I dont know why it works, it does ^^
What I would like is to create something to loop through this code with multiple items. I was thinking to create a string array.
Something like;
item(1) = "regio" 'Setting
item(2) = "city" 'Setting
item(3) = "postal" 'Setting
But I do not know how to start, could somebody help me in the correct direction?
I currently have this code to control a Pivottable with Listboxes in my Userform;
Code:
Dim DraaiTabel As PivotTable
Dim pf As PivotField
Dim LbItem As Long
'Activate correct sheet to be sure
Worksheets("Draaitabel").Activate 'draaitabel = PivotTable in Dutch
Set DraaiTabel = Sheets("Draaitabel").PivotTables("Draaitabel")
Dim i As Integer
Dim count As Integer
Dim lb As MSForms.ListBox
Dim item
Dim pi As PivotItem
'Region (in Dutch "Regio")
Set lb = Me.ListBox10 'Setting
item = "regio" 'Setting
Set pf = DraaiTabel.PivotFields(item)
count = 0
For i = 0 To lb.ListCount - 1
If lb.Selected(i) Then
count = count + 1
End If
Next i
If count = lb.ListCount Then 'If the are equal, then show all
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
Else 'If there not equal, turn everything of and only view selected items
For Each pi In pf.PivotItems
pi.Visible = False
For LbItem = 0 To Me.ListBox10.ListCount - 1
If Me.ListBox10.Selected(LbItem) = False Then
pf.PivotItems(Me.ListBox10.List(LbItem)).Visible = False
Else
pf.PivotItems(Me.ListBox10.List(LbItem)).Visible = True
End If
Next LbItem
Next pi
End If
The reason why I have coded it like this with a count is that there are empty field in my source data, when filtering just with the 'else' bit of the code the Empty rows remain in the pivot table next to the selected items.
What i've done is counting the selected items of the listbox and compare this with the number of items, when there equal, then the 'empty' items need to be shown, when there not equal, I only wan't to see the selected items. (so emptyItems.visible = false)
This works, while there is something 'weird' in the last bit, while it works at the time, I dont like the code that much, in a PivotTable it isnt possible to view nothing, so i've put the "Next pi" at the bottom. While I dont know why it works, it does ^^
What I would like is to create something to loop through this code with multiple items. I was thinking to create a string array.
Something like;
item(1) = "regio" 'Setting
item(2) = "city" 'Setting
item(3) = "postal" 'Setting
But I do not know how to start, could somebody help me in the correct direction?