Disable checkbox in VBA

ElvisSteel

Board Regular
Joined
Mar 23, 2009
Messages
122
I have a sheet with a number of checkboxes, and I need to conditionally enable/disable some of them.
For example, if the user checks Box1, then the question for Box2 becomes relevant and Box2 becomes available. If Box1 is unchecked, Box2 needs to be unchecked and disabled.
Is this possible and if so, can you please advise how.
Thanks

Steve
 
It would be nice to have a generic routine which you can apply to all of the checkboxes (except for the last checkbox which has no dependants), otherwise you would have to write one routine per checkbox. Assuming you have them laid out on the worksheet in the order they were added, you could use something like this:

Code:
Sub CheckboxManager()
    Dim i As Long
    Dim lngItem As Long
    Dim blnTicked As Boolean
    
    With Sheet1.CheckBoxes
        
        'which checkbox was changed?
        lngItem = .Item(Application.Caller).Index
        
        'is it ticked?
        blnTicked = (.Item(lngItem).Value = 1)
        
        'if it was ticked then
        'we want to enable the following checkbox
        If blnTicked Then
            .Item(lngItem + 1).Enabled = True
            
        'if it was unticked then
        'we want to disable all the subsequent checkboxes and untick them
        Else
            For i = (lngItem + 1) To .Count
                With .Item(i)
                    .Value = False
                    .Enabled = False
                End With
            Next i
        End If
    End With
    
End Sub
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
If they were arranged in a regular pattern, you could also do something using the TopLeftCell, which might be safer than the Index.
 
Upvote 0
Yeah, index relies on their position order being the same as their creation order.

I was looking at the possible checkbox values and it looks like there are three: Unchecked, (-4146) Checked (1) and Mixed (2). Do you know if there is a built-in class with these enumerated constants available?
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,447
Latest member
M V Arun

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