+ Reply to Thread
Results 1 to 7 of 7

Thread: Programming Checkboxes...

  1. #1
    Registered User
    Join Date
    04-06-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2003/2007
    Posts
    13

    Programming Checkboxes...

    I have a checkbox at the end of each row in the sheet, e.g.:

    For Each cell In Range("J5:J20")
        With Sheets(1).CheckBoxes.Add(cell.Left, cell.Top, 15, cell.Height)
            .LinkedCell = cell.Offset(, 0).Address(External:=True)
            .Interior.ColorIndex = xlClear
            .Caption = ""
        End With
    Next
    How do I assign an action to the boxes that will affect only the (Active?) row of that box, without having to write separate code for each individual checkbox? (For example: When a box is checked, the background color of the first cell in the row changes to red, and reverts if unchecked).

    Thank you.
    Last edited by Platem; 07-07-2011 at 01:10 AM.

  2. #2
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: Programming Checkboxes...

    Platem,

    The method you showed adds Form Control checkboxes. You can assign the following macro to all of them. This macro simply turns the corresponding cell in column A red if checked, and clears the red if unchecked:
    Sub Chk_Click()
        
        Dim chk As CheckBox: Set chk = ActiveSheet.CheckBoxes(Application.Caller)
        If chk.Value = xlOn Then
            Cells(chk.TopLeftCell.Row, 1).Interior.ColorIndex = 3
        Else
            Cells(chk.TopLeftCell.Row, 1).Interior.ColorIndex = 0
        End If
        
    End Sub


    Hope that helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    04-06-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2003/2007
    Posts
    13

    Re: Programming Checkboxes...

    Thanks tigeravatar.

    How do I assign the macro automatically to each checkbox as it is created? My worksheet may have up to 200 checkboxes!

  4. #4
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: Programming Checkboxes...

    Platem,

    In the macro code you posted, add the following line after .Caption = "" and before End With:
        .Caption = ""
        .OnAction = "Chk_Click"
    End With


    Hope that helps,
    ~tigeravatar

  5. #5
    Registered User
    Join Date
    04-06-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2003/2007
    Posts
    13

    Re: Programming Checkboxes...

    I only had to change that to:

    .OnAction = "ThisWorkbook.Chk_Click"
    Again, thanks a million!

  6. #6
    Registered User
    Join Date
    04-06-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2003/2007
    Posts
    13

    Re: Programming Checkboxes...

    I've tried to move the above snippet into my project but it is causes this error:

    Run-time error '1004': Unable to set the ColorIndex property of the Interior class


    The following segment I place in the 'ThisWorkbook Workbook_Open()' module (should run only at start):
        .
        .
    	 		
        'add some checkboxes
        For Each cell In Range("O3:O71,O77:O144")
           With Sheets(1).CheckBoxes.Add(cell.Left, cell.Top, 15, cell.Height)
               .LinkedCell = cell.Offset(, 0).Address(External:=True)
               .Interior.ColorIndex = xlClear
               .Caption = ""
               .OnAction = "Chk_Click"
           End With
        Next
        .
        .
        .
    ...and this segment appears in the Modules section (as Module3):
    Sub Chk_Click()
        Dim chk As CheckBox: Set chk = ActiveSheet.CheckBoxes(Application.Caller)
        If chk.Value = xlOn Then
            Cells(chk.TopLeftCell.Row, 1).Interior.ColorIndex = 3
        Else
            Cells(chk.TopLeftCell.Row, 1).Interior.ColorIndex = 0
        End If
    End Sub
    Last edited by Platem; 07-06-2011 at 07:08 PM.

  7. #7
    Registered User
    Join Date
    04-06-2011
    Location
    NY, USA
    MS-Off Ver
    Excel 2003/2007
    Posts
    13

    Re: Programming Checkboxes...

    Solved it!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0