vba - combo box to change filters on excel pivot table -
excel 2013. using 3 combo boxes change filters on pivot table. first combo box has "project1", "project2" & all. second combo box has "customer1", "customer2" & all. third combo box has "country1", "country2" & all.
i using 9 pivot tables, of them have filters [project], [customer], [country].
my intention change first combo box project1 & pivot tables filter should change project1.i able that.
however when select first combo box "all". first combo box cell link y1. vba run time error 1004: application-defined or object-defined error.
sub projectname() activesheet.pivottables("pvt1").pivotfields("project name").clearallfilters activesheet.pivottables("pvt2").pivotfields("project name").clearallfilters activesheet.pivottables("pvt3").pivotfields("project name").clearallfilters activesheet.pivottables("pvt1").pivotfields("project name").currentpage = range("y1").text activesheet.pivottables("pvt2").pivotfields("project name").currentpage = range("y1").text activesheet.pivottables("pvt3").pivotfields("project name").currentpage = range("y1").text
since first 3 lines of code go without issue, assume pivot table pvt1
, field project name
exist. places error somewhere after that.
for call .currentpage
1004 error following reasons:
- using try , filter field not set
report filter
. cannot usecurrentpage
filter rows or columns - setting value not exist in list of possible values
on second point, call range
might relevant.
- verify value there exists in list of possible ones.
- also aware using
.text
use display value of cell , not underlying.value
to resolve these issues, there couple of options:
- for case want filter data on row or column (and not in filters section) can go through
pivotitems
, setvisible = true/false
- you can set label filter vba if want instead of manual filter
- if want check value existing in
currentpage
, can iteratepivotitems
pivotfield
, check 1 matches. code similarfor each
loop check on value, don't setvisible
.
code setting filter on row or column
sub filterpivotfield() dim pt pivottable set pt = activesheet.pivottables("pvt1") dim pf pivotfield set pf = pt.pivotfields("c") pf.clearallfilters 'slow iterates items , sets visible (manual filter) dim pi pivotitem each pi in pf.pivotitems pi.visible = (pi.name = range("j2")) next 'fast way sets label filter pf.pivotfilters.add2 type:=xlcaptionequals, value1:=range("j2") end sub
picture of ranges
Comments
Post a Comment