Toggling a Checkbox VBA

termeric

Active Member
Joined
Jun 21, 2005
Messages
257
I have a quick question about checkboxes. I am trying to change the values of checkboxes on a tab using VBA. I have one piece of code working that triggers when certain cells change, and updates the value of the checkbox accordingly. this code is located on the worksheet. i have another section of code located in a module that does not work. In both instances, i am referring to my checkboxes as me.CbName. do i need to refer to them differently when i have my code stored in a module?


This code does not work (stored in a module)
Code:
Sub deleteme()
Dim xyz As Boolean
xyz = True

If xyz = True Then Me.CBRates = True

End Sub

This is the code that works (stored on the worksheet)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("C4:E50")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

' Display a message when one of the designated cells has been
        ' changed.
        ' Place your code here.
        'MsgBox "Cell " & Target.Address & " has changed."


    If Target.Address = "$C$4" Then
        
        
    ElseIf Target.Address = "$C$5" Then
        'Census Views?
        If Target.Value = "Yes" Then
            Sheets("Census").Visible = True
            Me.CbCensus = True
        Else
            Sheets("Census").Visible = False
            Me.CbCensus = False
            Call ImportScript
            Call ShowRow
            Call ShowCol
            'MsgBox "Be Sure to Import the Census"
        End If
      
        
    ElseIf Target.Address = "$C$6" Then
        If Sheets("Case Info").Range("C6").Value = "No" And Sheets("Case Info").Range("C7").Value = "No" Then
            Sheets("Current Benefits").Visible = False
            Me.CBCurrentBen = False
        Else
            Sheets("Current Benefits").Visible = True
            Me.CBCurrentBen = True
        End If
        
    ElseIf Target.Address = "$C$7" Then
        If Sheets("Case Info").Range("C6").Value = "No" And Sheets("Case Info").Range("C7").Value = "No" Then
            Sheets("Current Benefits").Visible = False
            Me.CBCurrentBen = False
        Else
            Sheets("Current Benefits").Visible = True
            Me.CBCurrentBen = True
        End If
        
    ElseIf Target.Address = "$C$8" Then
        'Are there custom plans?
        If Target.Value = "Yes" Then
            Sheets("Custom Benefit Request").Visible = True
            Me.CBCustom = True
            'MsgBox "Be Sure to Import the Census"
        Else
            Sheets("Custom Benefit Request").Visible = False
            Me.CBCustom = False
            ImportScript
        End If
        
    ElseIf Target.Address = "$E$8" Then
        'How Many?
        ShowColCustom
    
    ElseIf Target.Address = "$C$39" Then
        If Target.Value = "Commission" Then
            Rows("40:42").EntireRow.Hidden = False
        ElseIf Target.Value = "PSF" Then
            Rows("40:41").EntireRow.Hidden = False
            Rows("42:42").EntireRow.Hidden = True
        Else
            Rows("40:42").EntireRow.Hidden = True
        End If
        
    ElseIf Target.Address = "$C$43" Then
        If Target.Value = "Yes" Then
            Rows("44:48").EntireRow.Hidden = False
        Else
            Rows("44:48").EntireRow.Hidden = True
        End If
    
    
    End If





End If
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

6StringJazzer

Well-known Member
Joined
Jan 27, 2010
Messages
857
Office Version
  1. 365
Platform
  1. Windows
Me has no meaning when used in a standard Module. It will cause a compile error. You have to use the codename or tab name of the worksheet you want to refer to. One of the following:

VBA Code:
If xyz = True Then Sheet1.CBRates = True

If xyz = True Then Worksheets("My Sheet").CBRates = True
 

termeric

Active Member
Joined
Jun 21, 2005
Messages
257
thank you
Me has no meaning when used in a standard Module. It will cause a compile error. You have to use the codename or tab name of the worksheet you want to refer to. One of the following:

VBA Code:
If xyz = True Then Sheet1.CBRates = True

If xyz = True Then Worksheets("My Sheet").CBRates = True
 

Watch MrExcel Video

Forum statistics

Threads
1,132,646
Messages
5,654,567
Members
418,142
Latest member
peterappiahkubi

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
Top