ActiveX CheckBox problem

CrispyAsian

Board Regular
Joined
Sep 22, 2017
Messages
64
Hello everyone,

I am having an issue with ActiveX Checkboxes but I don't know what the problem is or how to fix it. So I have a sheet like this:

Sheet_example.jpg
[/URL][/IMG]

And I have a code running to make everyone in column B have their own sheet. However, I only want that to happen if the checkbox in column A is checked. So far I have this running:

Code:
Sub Exampleofmycode

Dim objControl As Object
Dim lastrow As String

ThisWorkbook.Sheets("Personnel").Activate

lastrow = ActiveSheet.Columns("B").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

For Each c In ThisWorkbook.Sheets("Personnel").Range("B3:B" & lastrow)
    If c.Value <> "" Then
        For Each objControl In Sheets("Personnel").OLEObjects
            If TypeName(objControl.Object) = "CheckBox" Then
[COLOR=#ff0000]                If objControl.TopLeftCell.Row = c.Row Then[/COLOR]
                    If objControl.Object.Value = 1 Then
                         'Rest of the code goes here
                   End If
               End If
            End If
        Next 
     End If
Next c

End Sub

I ran through it and it never makes it past the line in red. It just jumps down to the End If. Does anybody know what I'm doing wrong?
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
hi,
untested but see if this update to your code helps


Code:
Sub Exampleofmycode()


    Dim objControl As Object
    Dim lastrow As Long
    Dim c As Range
    
    With ThisWorkbook.Sheets("Personnel")
            .Activate
    lastrow = .Columns("B").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    
    For Each c In .Range("B3:B" & lastrow)
        If c.Value <> "" Then
            For Each objControl In .OLEObjects
                If TypeName(objControl.Object) = "CheckBox" Then
                    If objControl.Top = c.Top Then
                        If objControl.Object.Value Then
                        
                             'Rest of the code goes here
                       End If
                   End If
                End If
            Next
         End If
    Next c
    End With
End Sub

Dave
 
Upvote 0
Why not use Forms checkboxes?

If you did then you could use Application.Caller to identify the checkbox that has been checked/unchecked.
 
Upvote 0
An alternative method...

You could avoid the complication of check boxes

To test - create a new workbook
Place code below in the sheet module
Click in any cell in column A below row 2
Cell behaves like a check box

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.CountLarge = 1 And Target.Column = 1 And Target.Row > 2 Then
        If Target = "a" Then Target.ClearContents Else: Target = "a"
        Target.Font.Name = "Marlett"
        Target.Offset(, 1).Activate
    End If
End Sub

(Instead of checking to see if checkbox is ticked, test if value of cell is "a")
 
Upvote 0
@dmt32 I tried your code, but it didn't work. Thanks for trying though.
@Norie I was using Form Controls but they gave me even bigger headaches. Also, I don't want the code to run when the checkbox is marked, but when the code does run I need it to check all the check boxes in comparison to the person's name.
@Yongle What an interesting idea. Very out-of-the-box style. I liked it a lot and decided to use it. I made a couple edits to make the code a little more appealing to look at. Also, I wasn't a huge fan of the Offset so I just took it out. This is what I'm using:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.CountLarge = 1 And Target.Column = 1 And Target.Row > 2 Then
        If Target = ChrW(&H2713) Then
        Target = "X"
        Target.Font.ColorIndex = 3
        Else
        Target = ChrW(&H2713)
        Target.Font.ColorIndex = 10
        Target.HorizontalAlignment = xlHAlignCenter
        End If
    End If
End Sub

I would have never thought of something like that myself. Thanks a lot, it's working perfectly.
 
Last edited:
Upvote 0
Thanks for the feedback - glad the code is useful
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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