How to hide checkbox when there is a value in a different sheet?

Netha Lawrence 0 Reputation points
2024-02-13T20:48:35.5466667+00:00

I am trying to hide a check box when the value is True from a different sheet. I have tried a bunch of things and I'm not sure what is wrong. My current code looks like this: Sub CheckBox1() Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = Worksheets(Corrigan_TaskList).Range("E11").Value Then If Traget.Value = "True" Then ActiveSheet.Shapes("CheckBox1").Visible = False Else ActiveSheet.Shapes("CheckBox1").Visible = True End If End If End Sub

Microsoft 365 and Office Development Other
Microsoft 365 and Office Excel For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2024-02-13T22:29:01.7966667+00:00

    Without seeing your worksheet we're sort of guessing how you set this up. Assuming you added a checkbox from the Developers tab to the sheet then you can do what you're asking like this.

    Private Sub Worksheet_Change(ByVal Target As Range)
       ' Only do this if the correct cell value has changed  
       If Target.Address <> "$E$11" Then
          Exit Sub
       End If
       
       Dim targetValue As String
       targetValue = Target.Value
       
       Dim sheet As Worksheet
       Set sheet = ThisWorkbook.Sheets("Sheet1")
       
       ' Use the checkbox label here
       ' This is doing simple string comparison, may need to be more complex
       sheet.CheckBoxes("Check Box 1").Visible = (targetValue = "True")
    End Sub
    

    Note that the active worksheet is the worksheet where a change was detected. So you have to reach into the other worksheet to get to the checkbox. Note also that you have to know what the name of that checkbox is to get this to work.

    I find this behavior confusing myself as it only toggles if you make a change on another worksheet, it won't work if the first sheet is the one with the checkbox and making something appear and disappear is confusing. I might recommend that you just disable it (Enabled) instead of hiding it.

    You'll also need to replicate the logic when the sheet is shown (Activated). At that point it might make more sense to just have the Activated event check the condition and show/hide rather than reacting to a change event on another worksheet. Then it'll behave correctly irrelevant of which sheet is shown first.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.