Excel VBA - loop through Form Control checkbox not working -
so whatever reason, i'm having trouble getting vba code loop through form control checkboxes on worksheet in order have 'select all' check box. came across following 2 different methods supposedly work in doing this:
method 1:
sub selectall_click() dim cb checkbox each cb in activesheet.checkboxes if cb.name = activesheet.checkboxes("cbsiteall").name msgbox cb.name & ": " & cb.value, vbokonly cb.value = activesheet.checkboxes("cbsiteall").value else msgbox cb.name & ": " & cb.value, vbokonly end if next cb end sub
method 2:
sub selectall_click() dim cb shape dim sh worksheet set sh = activesheet each cb in sh.shapes if cb.type = msoformcontrol if cb.formcontroltype = xlcheckbox msgbox cb.name, vbokonly if cb.name <> application.activesheet.checkboxes("cbsiteall").name cb.value = application.activesheet.checkboxes("cbsiteall").value msgbox xcheckbox.name & ": " & xcheckbox.value, vbokonly end if end if end if next cb end sub
the message boxes entered try , debug happening. both of above, loop starts message box relates box ticked i've assigned macro. not appear loop through of other checkboxes (although validation checkbox works loop @ least recognises item ticked/unticked checkbox.
i have no idea why either of these not working given above , i've spent fair bit of time looking answers address specific issue , working through logic myself no avail. alas, hand on internet see if can help.
thanks in advance.
for benefit of others coming across post, shape
object contains groupitems
property. property, still shape
object, collection of shapes (https://msdn.microsoft.com/vba/excel-vba/articles/shape-groupitems-property-excel). when iterate shapes
list, access 'top-level' shapes; in other words, grouped shapes can accessed via groupitems
property.
if have grouped shapes on worksheet wish include in for each...
loop, 1 solution iterate recursively. code below:
option explicit public sub runme() recursiveloop sheet1.shapes end sub private sub recursiveloop(col object) dim shp shape each shp in col if isgrouped(shp) recursiveloop shp.groupitems else debug.print shp.name end if next end sub private function isgrouped(shp shape) boolean dim grp groupshapes on error resume next set grp = shp.groupitems on error goto 0 isgrouped = (not grp nothing) end function
Comments
Post a Comment