Set Pivot items on multiple (numeric value) cells

remonvdd

New Member
Joined
Nov 13, 2015
Messages
2
Hi,

I am struggling with a macro to set multiple pivot items based on multiple cells. I have tried different codes but they don't work as they should be.
In this case, I would like the macro to set the months based on cells "'Validatie'!AQ2" and "'Validatie'!AQ3". These cells contain numeric values (month numbers).

For example, the cells contain the values 10 (AQ2) and 9 (AQ3). I want the macro to only select the values 9 and 10 as Pivot item within the pivot field "Maand".

The code that I have (it outputs the value fron AQ2):

Code:
Sub filter_mom()


Dim item As String, it As PivotItem
item = Range("'Validatie'!AQ2, 'Validatie'!AQ3").Value


ActiveSheet.PivotTables("Draaitabel1").PivotFields("Maand"). _
        ClearAllFilters


For Each it In ActiveSheet.PivotTables("Draaitabel1").PivotFields("Maand").PivotItems
    If item = it.Name Then
        it.Visible = True
    Else
        it.Visible = False
    End If
Next it


End Sub

Thanks for your help!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try...

Code:
[color=darkblue]Sub[/color] filter_mom()

    [color=darkblue]Dim[/color] vPivotItemValue [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    [color=darkblue]Dim[/color] oPivotItem [color=darkblue]As[/color] PivotItem
    [color=darkblue]Dim[/color] rItems [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] rItem [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] bFound [color=darkblue]As[/color] [color=darkblue]Boolean[/color]
    
    [color=darkblue]Set[/color] rItems = Range("'Validatie'!AQ2, 'Validatie'!AQ3")
    
    [color=darkblue]With[/color] ActiveSheet.PivotTables("Draaitabel1").PivotFields("Maand")
        .ClearAllFilters
        [color=darkblue]For[/color] [color=darkblue]Each[/color] oPivotItem [color=darkblue]In[/color] .PivotItems
            [color=darkblue]If[/color] IsNumeric(oPivotItem.Value) [color=darkblue]Then[/color]
                vPivotItemValue = [color=darkblue]CDbl[/color](oPivotItem.Value)
            [color=darkblue]Else[/color]
                vPivotItemValue = oPivotItem.Value
            [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]For[/color] [color=darkblue]Each[/color] rItem [color=darkblue]In[/color] rItems
                [color=darkblue]If[/color] rItem.Value = vPivotItemValue [color=darkblue]Then[/color]
                    bFound = [color=darkblue]True[/color]
                    [color=darkblue]Exit[/color] [color=darkblue]For[/color]
                [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]Next[/color] rItem
            [color=darkblue]If[/color] bFound [color=darkblue]Then[/color]
                bFound = [color=darkblue]False[/color]
            [color=darkblue]Else[/color]
                oPivotItem.Visible = [color=darkblue]False[/color]
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Next[/color] oPivotItem
    [color=darkblue]End[/color] [color=darkblue]With[/color]
        
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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