excel - Iterate over slicer via VBA and select a single item each time -


i have several slicers within spreadhseet. i'd able loop on 1 of these via vba, , select every option 1 one. macro below seems fine tired eyes, doesn't work when run it. when add breakpoint @ 'breakpoint tag here below, first item gets selected, macro goes second 1 while keeping first 1 selected , end items selected...

    sub slicers(slname string)     dim slitem sliceritem, sldummy sliceritem     dim slbox slicercache     set slbox = activeworkbook.slicercaches(slname)     each slitem in slbox .sliceritems       each sldummy in slbox .sliceritems           sldummy.selected = (sldummy.name = slitem.name)       next sldummy     next slitem 'breakpoint     end sub      sub test()     call slicers("a_slicer_name")     end sub 

thanks help

edit:
pointed out scott holtzman, turns out need clear filter (slbox.clearmanualfilter) upon selecting new item.

why necessary whereas boolean test works when debug.print it?

the code below works fine:

    sub slicers(slname string)     dim slitem sliceritem, sldummy sliceritem     dim slbox slicercache     set slbox = activeworkbook.slicercaches(slname)     each slitem in slbox .sliceritems       slbox.clearmanualfilter 'this line needed add       each sldummy in slbox .sliceritems           sldummy.selected = (sldummy.name = slitem.name)       next sldummy     next slitem     end sub      sub test()     call slicers("a_slicer_name")     end sub 

since question linked in comment not have accepted answer (the user never choose answer or reply suggestion), provide solution problem here well.

sub slicers(slname string)      dim slitem sliceritem, sldummy sliceritem     dim slbox slicercache      set slbox = activeworkbook.slicercaches(slname)      'loop through each slicer item     each slitem in slbox.sliceritems          'show items start         slbox.showallitems 'or .clearmanualfilter          'test each item against         each sldummy in slbox.sliceritems              'if item equals item in first loop, select             'otherwise don't show (thus showing 1 @ time between nested loops)             if slitem.name = sldummy.name sldummy.selected = true else: sldummy.selected = false              'more code process data (i suspect)          next sldummy      next slitem  end sub 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -