VBA Cycle through visible checkboxes and hide if unchecked

Gti182

Board Regular
Joined
Dec 7, 2011
Messages
65
Hi All,

I'm looking for some code to cycle through the visible checkboxes in a sheet and only hide those which are not ticked.

What adds a level of complexity is some checkboxes may already be hidden
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
That's a strange one as once they are unchecked, how will they ever become visible again if you need to check them?
 
Upvote 0
This will do as you ask any way:

Code:
Sub SetCheckboxes()
    Dim cb As CheckBox
    
    For Each cb In ActiveSheet.CheckBoxes
        cb.Visible = cb.Value = 1
    Next
    
End Sub
 
Last edited:
Upvote 0
Not really an issue as I intend to add the hide visible checkboxes code to a separate send to pdf bit of code I already have in place. The send to PDF code will only be executed by the user once they are happy with their checkbox selections.
 
Last edited:
Upvote 0
This seems to unhide checkboxes that were already hidden.

For my example. I have 20 checkboxes, if a scenario A is selected 10 of these are hidden. For the remaining 10 checkboxes which are visible lets say 6 are ticked, i'd like to hide just those 4 unticked ones.
 
Upvote 0
in my head the code would go something like this but can't quite figure out the exact syntax


Code:
For each visible checkbox between i to 20.
If checkbox(i).value = 0 'if unticked
then checkbox.visible = False
End if
Next
 
Last edited:
Upvote 0
in my head the code would go something like this but can't quite figure out the exact syntax


Rich (BB code):
For each visible checkbox between i to 20.
If checkbox(i).value = 0 'if unticked
then checkbox.visible = False
End if
Next

Code:
[COLOR=#333333]Sub SetCheckboxes()[/COLOR]    Dim cb As CheckBox
    
    For Each cb In ActiveSheet.CheckBoxes
        if cb.visible = true then cb.Visible = cb.Value = 1
    Next

[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Code:
[COLOR=#333333]Sub SetCheckboxes()[/COLOR]    Dim cb As CheckBox
    
    For Each cb In ActiveSheet.CheckBoxes
        [B][COLOR="#FF0000"]if cb.visible = true then cb.Visible = cb.Value = 1[/COLOR][/B]
    Next

[COLOR=#333333]End Sub[/COLOR]
You can also write the highlighted line of code this way...

cb.Visible = cb.Value * cb.Visible = -1
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top