Excel Slicer Manipulation with VBA -


i learning how create , manipulate excel slicers vba. have been following examples paul te braack on "business intelligence website: https://paultebraak.wordpress.com/2012/02/24/accessing-the-slicer-through-vba/

when try cycle through items of slicer cache , print values of "caption", "value" , "name" in immediate window, returns error of "run-time error '1004': application-defined or object-defined error line: " code: "set sl = sc.slicercachelevels(1)"

sub btest() dim sc slicercache dim sl slicercachelevel dim si sliceritem  application.enableevents = false application.calculation = xlmanual set sc = activeworkbook.slicercaches(5) set sl = sc.slicercachelevels(1) debug.print “——————————————————————————”  each si in sl.sliceritems debug.print “caption; – > ” & si.caption debug.print “value; – > ” + cstr(si.value) debug.print “unique; name; – > ” + si.name debug.print “——————————————————————————”  next application.enableevents = true application.calculation = xlcalculationautomatic  end sub 

i modified code, modified code returns results "false," "false," , "sheet5false" returned. pivot table , slicer in sheet5 way. (which think should work based on slicer cache , item verifications mentioned below.)

when review slicer cached in locals window, shows has 30 items, , can expand items , display values of caption, value, , name.

when type "debug.print sc.sliceritems.count" in immediate window returns answer of 30. when type "debug.print sc.sliceritems(2).name in immediate window returns answer of 30.

the modified code is:

sub ctest() dim sc slicercache dim sl slicercachelevel dim si sliceritem  application.enableevents = false application.calculation = xlmanual set sc = activeworkbook.slicercaches(5) set si = sc.sliceritems(1) debug.print “——————————————————————————”  each si in sc.sliceritems debug.print “caption; – > ” & si.caption debug.print “value; – > ” + cstr(si.value) debug.print “unique; name; – > ” + si.name debug.print “——————————————————————————”  next application.enableevents = true application.calculation = xlcalculationautomatic  end sub 

any guidance on doing wrong in original code example or modified code example be

sliceritemlevels applicable olap data feeds. see reference.

it seems website copied code used ascii characters 147 (“) & 148 (”) open , close quotes. vba expects ascii character 34 (") both. following should work fine.

sub ctest() dim sc slicercache dim sl slicercachelevel dim si sliceritem  application.enableevents = false application.calculation = xlmanual set sc = activeworkbook.slicercaches(5) set si = sc.sliceritems(1) debug.print "——————————————————————————"  each si in sc.sliceritems debug.print "caption; – > " & si.caption debug.print "value; – > " + cstr(si.value) debug.print "unique; name; – > " + si.name debug.print "——————————————————————————"  next application.enableevents = true application.calculation = xlcalculationautomatic  end sub 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -