excel - Run all possible combination of page filters in a pivot + VBA + Dynamic Solution -
i trying create macro dynamic , runs through possible combination of page filters , produce reports.
right now, have 2 filters: accountmanager , costcenter , macro below runs through values of accountmanager , corresponding value of costcenter , prepares report.
sub run_all_reports() dim pt pivottable dim pf pivotfield dim pi pivotitem, pi2 pivotitem sheets("pivot").activate set pt = activesheet.pivottables("budget") each pi in pt.pagefields(1).pivotitems pt.pagefields(1).currentpage = pi.name each pi2 in pt.pagefields(2).pivotitems pt.pagefields(2).currentpage = pi2.name call run_report next next end sub
i not know how extend functionality dynamic, i.e. reads how many page filters present , prepares report each possible combination.
lets say, introduce filter - area. should produce reports possible combination. example below:
john, marketing, london john, marketing, newyork john, sales, london sam, sales, london sam, sales, newyork
not sure if clear enough. have tried on couldn't find solution. got asked question 2 page filters , have implemented above solution have headache since yesterday because want dynamic in head.
use recursive procedure, following
e.g.
sub runforallitems(pt pivottable, count integer) numfields= pt.pagefields.count each pi in pt.pagefields(num).pivotitems pt.pagefields(num).currentpage = pi.name if (num=numfields) call run_report elseif (num<numfields) call runforallitems(pt, count+1) end if next end sub
and call with
runforallitems(pt, 1)
i haven't tested it, should - minor tweaking - work
Comments
Post a Comment