Hide and uncheck checkbox based on cell value

povictory

New Member
Joined
May 28, 2015
Messages
41
Hello everyone!

I have a sheet where I have some checkboxes that I want to appear/disappear based on an input in another cell. I found the thread and code below that solved that for me, however I am also wanting the boxes to be unchecked in the case where they disappear. I figured it would be a fairly simple modification to the code below, even for a newbie like me, but anything I've tried seems to break it and I can't figure out a way that works. Any input on what modifications I can make to the code below to automatically uncheck the boxes when K9 does *not* = "Yes" would be greatly appreciated!

Here is the original thread:

Here is the original code that that I would like to modify:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If ActiveSheet.Range("k29").Value = "Yes" Then
        ActiveSheet.Shapes("CheckBox1").Visible = True
        ActiveSheet.Shapes("CheckBox2").Visible = True
    Else
        ActiveSheet.Shapes("CheckBox1").Visible = False
        ActiveSheet.Shapes("CheckBox2").Visible = False
    End If
   
    If ActiveSheet.Range("k26").Value = "Yes" Then
        ActiveSheet.Shapes("CheckBox3").Visible = True
    Else
        ActiveSheet.Shapes("CheckBox3").Visible = False
    End If
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Any input on what modifications I can make to the code below to automatically uncheck the boxes when K9 does *not* = "Yes"
The OP in the linked thread says their checkboxes are Form Control checkboxes, rather than ActiveX Control checkboxes, however you haven't clarified your type. Assuming they're Form Control checkboxes then try this code in the sheet module of the sheet where the checkboxes reside:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With ActiveSheet
        If .Range("K9").Value = "Yes" Then
            .CheckBoxes("Check Box 1").Visible = True
            .CheckBoxes("Check Box 2").Visible = True
        Else
            'Hide and uncheck check boxes
            .CheckBoxes("Check Box 1").Visible = False
            .CheckBoxes("Check Box 1").Value = xlOff
            .CheckBoxes("Check Box 2").Visible = False
            .CheckBoxes("Check Box 2").Value = xlOff
        End If
    End With
End Sub
Note - your description says K9, but your code is looking at K29.
 
Upvote 0
Solution
The OP in the linked thread says their checkboxes are Form Control checkboxes, rather than ActiveX Control checkboxes, however you haven't clarified your type. Assuming they're Form Control checkboxes then try this code in the sheet module of the sheet where the checkboxes reside:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With ActiveSheet
        If .Range("K9").Value = "Yes" Then
            .CheckBoxes("Check Box 1").Visible = True
            .CheckBoxes("Check Box 2").Visible = True
        Else
            'Hide and uncheck check boxes
            .CheckBoxes("Check Box 1").Visible = False
            .CheckBoxes("Check Box 1").Value = xlOff
            .CheckBoxes("Check Box 2").Visible = False
            .CheckBoxes("Check Box 2").Value = xlOff
        End If
    End With
End Sub
Note - your description says K9, but your code is looking at K29.
You are correct that they are Form Control checkboxes and this works perfectly! Also, apologies as I did miskey K9 instead of K29.

Thank you so much for this solution...it's much appreciated!
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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