excel - VBA Filter error -
i have filter field called week_of_year (1,2,3,4,5,6,7,8....), pivot field year(2012,2013,2014), vba code belows filter on current week , year of 2014/2015 , hide else. problem code loops each record , runs slow when comes big pivot table. im trying on new code encountering error.
sub datefilter() dim pvttbl pivottable set pvttbl = worksheets("lo").pivottables("pivottable3") dim dd integer dd = format(date, "ww") dim pf pivotfield dim pf1 pivotfield dim pi pivotitem set pf = worksheets("lo").pivottables("pivottable3").pivotfields("week_of_year") set pf1 = worksheets("lo").pivottables("pivottable3").pivotfields("year") pvttbl.clearallfilters each pi in pf.pivotitems if pi.name = cstr(dd) pi.visible = true else pi.visible = false end if next each pi in pf1.pivotitems if pi.name = "2014" or pi.name = "2015" pi.visible = true else pi.visible = false end if next end sub new code develop is:
sub datefilter1() dim pvttbl pivottable set pvttbl = worksheets("lo").pivottables("pivottable3") dim dd integer dd = format(date, "ww") dim pf pivotfield set pf = worksheets("lo").pivottables("pivottable3").pivotfields("week_of_year") pf.pivotfilters.add2 xlvalueequals, 3 end sub the code failed @
pf.pivotfilters.add2 xlvalueequals, cstr(dd) i tried:
pf.pivotfilters.add2 type:= xlvalueequals, value1 := cstr(dd) the error invalid procedure call or argument.any idea how can fix error?????
any faster way filter on multiple filter criteria? thanks!
if want filter on 1 value, drag field of interest report filter pane, can without looping using following code:
sub filteronweek() dim pt pivottable dim pf pivotfield set pt = worksheets("lo").pivottables("pivottable3") set pf = pt.pivotfields("week_of_year") pf.currentpage = format(date, "ww") end sub if reason field absolutely must stay in pivottable row field, can still filter without looping following method if have excel 2010 or later
- make copy of pivottable, , drag 'week_of_year' field in copy report filter area
- set slicer on week_of_year field on 1 pivottable, , connect other pivottable via report connections dialog. (right click on slicer, select 'report connections')
- amend above code runs on 2nd pivottable.
the slicer sync 2 pivottables.
if want filter on more one value, drag field rows area, , use this:
sub filteronweek2() dim pt pivottable dim pf pivotfield dim pi pivotitem dim lstart long dim lend long lend = format(date, "ww") lstart = val(format(date, "ww")) - 1 set pt = worksheets("lo").pivottables("pivottable3") set pf = pt.pivotfields("week_of_year") pf .clearallfilters .pivotfilters.add type:=xlcaptionisbetween, value1:=lstart, value2:=lend end end sub if reason don't want field in pivottable row field, follow same general approach above: make copy of pivottable, put field want filter on in copy rows field, connect fields of interest slicer, hide slicer somewhere, , amend code runs on copy of pivottable rowfield in it.
i recommend read article @ http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/ if want learn more efficiently filtering pivottables.
Comments
Post a Comment