Hide Checkbox based on cell value

robertdodson1979

New Member
Joined
May 30, 2014
Messages
7
Hopefully this will be a quick fix question.

I am attempting to hide a dozen checkboxes based on the value of a cell being "1" (results of a true/false expression). This seems like such a simple task, but I am failing anyway. I realize it is probably a syntax issue, but I'm not seeing it, probably because I am pretty green on VBA. I DID search first, but results mostly referred to using another checkbox and such

Code:
Private Sub HideCheckboxes(ByVal Target As Range)
    If ActiveSheet.Range("B1").Value = "1" Then
        ActiveSheet.CheckBoxes("CheckBox1").Visible = False
    Else
        ActiveSheet.CheckBoxes("CheckBox1").Visible = True
    End If
End Sub

Thanks in advance for you time!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You haven't said what exactly doesnt work and on what line it fails.

Try without putting the "1" in quotes.

Code:
Private Sub HideCheckboxes(ByVal Target As Range)
    If ActiveSheet.Range("B1").Value = 1 Then
        ActiveSheet.CheckBoxes("CheckBox1").Visible = False
    Else
        ActiveSheet.CheckBoxes("CheckBox1").Visible = True
    End If
End Sub
 
Upvote 0
Sorry about that.

It does nothing, even after I exit the VBA window and change "B1" from "0" to "1" and back a couple times. No visible error. Also nothing if I try the "Run" button, just a window that wants me to pick a Macro to run

Also, I did try the "1" without quotes to no avail
 
Upvote 0
I have tried it in two ways
Code:
Sub changecheck()
 If ActiveSheet.Range("B1").Value = 1 Then
        ActiveSheet.Shapes("CheckBox1").Visible = False
    Else
        ActiveSheet.Shapes("CheckBox1").Visible = True
    End If
End Sub

or with the worksheet calculate event. Right click the sheet tab and select view code and paste this code in there
Code:
Private Sub Worksheet_Calculate()
    If ActiveSheet.Range("B1").Value = 1 Then
        ActiveSheet.Shapes("CheckBox1").Visible = True
    Else
        ActiveSheet.Shapes("CheckBox1").Visible = False
    End If
End Sub

Both codes work

Just ensure your checkbox is named "CheckBox1" and not "Check Box 1"
 
Upvote 0
I'm sorry for delay in responding. Things got a little hectic at work.

I'm not sure what I may have done wrong, but they didn't seem to function for me. (Yes, I verified the checkbox name)

However, thank you for mentioning the use of "Shapes" instead of the "Checkboxes" that I had used.

Using "Shapes" and a couple things that I scraped up from an old project of mine, it seems to be working as follows:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target = Range("A1") Then
        If ActiveSheet.Range("A1").Value = 1 Then
            ActiveSheet.Shapes("CheckBox1").Visible = False
        Else
            ActiveSheet.Shapes("CheckBox1").Visible = True
        End If
    End If
End Sub

That works for me when I change the value in "A1"

I then moved my reference value to "K1" and decided that I would like to make the routine run when a value is selected in "ComboBox1"

This did the trick for me:

Code:
Private Sub ComboBox1_Change()
    If ActiveSheet.Range("K1").Value = 1 Then
        ActiveSheet.Shapes("CheckBox1").Visible = False
    Else
        ActiveSheet.Shapes("CheckBox1").Visible = True
    End If
End Sub

Thanks for the nudge in the right direction! :)
 
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,173
Members
449,296
Latest member
tinneytwin

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