+ Reply to Thread
Results 1 to 16 of 16

How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Question How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    I'm not that knowledgeable with Excel, but I have to have this spreadsheet set up as a template to use for many projects.

    It needs to have a check box on the beginning of each of 250 or so rows, and when the checkbox is checked it turns the entire row green and when unchecked turns the entire row back to white.

    Is there any easy way to set this up?

    Thanks a TON in advance... I've been Googling this for hours...

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Add CheckBoxes and Add Code

    Do A test sheet first.
    Enter something in Column A then run this code
    Sub AddCheckboxes()
        Dim c As Range
        Dim CkBx As CheckBox
        Dim Rws As Long, Rng As Range
    
        Rws = Cells(Rows.Count, "A").End(xlUp).Row
    
        Set Rng = Range(Cells(1, 1), Cells(Rws, 1))
        For Each c In Rng.Cells
            c.Select
            Set CkBx = ActiveSheet.CheckBoxes.Add(c.Left, c.Top, c.Width, c.Height)
    
            With CkBx
                .Caption = ""
                .OnAction = "SetToGreen"
            End With
    
        Next c
    
    End Sub
    
    Sub SetToGreen()
    
        Dim CkBx As CheckBox
        Dim CkRw As Integer
        Dim CkRng As String
    
        CkName = Application.Caller
        Set CkBx = ActiveSheet.CheckBoxes(CkName)
    
        CkRw = CkBx.TopLeftCell.Row
        CkRng = "A" & CStr(CkRw)
    
        If CkBx.Value > 0 Then
            Range(CkRng).EntireRow.Interior.Color = vbGreen
    
        Else
            Range(CkRng).EntireRow.Interior.ColorIndex = xlNone
    
    
        End If
    
    End Sub

  3. #3
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Add CheckBoxes and Add Code

    Do i put this in Visual Basic and hit run? It has a run-time error '1004' when i do that...

    or am I suppose to do something different?

    Thanks
    Last edited by davesexcel; 04-12-2013 at 10:45 PM.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    Check out the Example.
    Click the button to make the check boxes the click a check box
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    So how do i get it to run on column 1 and rows 20-270? etc? which parameters do i change?

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    So it works now?

  7. #7
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    yeah... it worked! thanks! now I just gotta figure out how to easily place it...

  8. #8
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    Well, the code you gave me works.. the file i downloaded of yours doesnt, it's a button that doesnt work...

  9. #9
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    Quote Originally Posted by Domeno View Post
    Well, the code you gave me works.. the file i downloaded of yours doesnt, it's a button that doesnt work...
    Did you enable Macros?

  10. #10
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    So if I modify the code to this:

    Sub AddCheckboxes()
        Dim c As Range
        Dim CkBx As CheckBox
        Dim Rws As Long, Rng As Range
    
        Rws = Cells(Rows.Count, "A").End(xlUp).Row
    
        Set Rng = Range(Cells(1, 20), Cells(Rws, 1))
        For Each c In Rng.Cells
            c.Select
            Set CkBx = ActiveSheet.CheckBoxes.Add(c.Center, c.Middle, c.Width, c.Height)
    
            With CkBx
                .Caption = ""
                .OnAction = "SetToGreen"
            End With
    
        Next c
    
    End Sub
    
    Sub SetToGreen()
    
        Dim CkBx As CheckBox
        Dim CkRw As Integer
        Dim CkRng As String
    
        CkName = Application.Caller
        Set CkBx = ActiveSheet.CheckBoxes(CkName)
    
        CkRw = CkBx.TopLeftCell.Row
        CkRng = "A" & CStr(CkRw)
    
        If CkBx.Value > 0 Then
            Range(CkRng).EntireRow.Interior.Color = vbGreen
    
        Else
            Range(CkRng).EntireRow.Interior.ColorIndex = xlNone
    
    
        End If
    
    End Sub
    Would that create the checkbox on A20? and place it Centered in Middle of cell?

    How would I get it to do like A20 through A270? and Centered in Middle of cell?

  11. #11
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    K.. that code i did doesnt work

    I'm confused on this code. What controls where it starts? It seems you can only control how many across or how many down.. but not what cell to start in...

    Am I missing something?

    Also, changing the "c. Left, c.Top " throws an error... so what controls the alignment in the cell?


    Quote Originally Posted by Domeno View Post
    So if I modify the code to this:

    Sub AddCheckboxes()
        Dim c As Range
        Dim CkBx As CheckBox
        Dim Rws As Long, Rng As Range
    
        Rws = Cells(Rows.Count, "A").End(xlUp).Row
    
        Set Rng = Range(Cells(1, 20), Cells(Rws, 1))
        For Each c In Rng.Cells
            c.Select
            Set CkBx = ActiveSheet.CheckBoxes.Add(c.Center, c.Middle, c.Width, c.Height)
    
            With CkBx
                .Caption = ""
                .OnAction = "SetToGreen"
            End With
    
        Next c
    
    End Sub
    
    Sub SetToGreen()
    
        Dim CkBx As CheckBox
        Dim CkRw As Integer
        Dim CkRng As String
    
        CkName = Application.Caller
        Set CkBx = ActiveSheet.CheckBoxes(CkName)
    
        CkRw = CkBx.TopLeftCell.Row
        CkRng = "A" & CStr(CkRw)
    
        If CkBx.Value > 0 Then
            Range(CkRng).EntireRow.Interior.Color = vbGreen
    
        Else
            Range(CkRng).EntireRow.Interior.ColorIndex = xlNone
    
    
        End If
    
    End Sub
    Would that create the checkbox on A20? and place it Centered in Middle of cell?

    How would I get it to do like A20 through A270? and Centered in Middle of cell?

  12. #12
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    Cells(rows,columns)
    if you want it to start at row 20 then cells(20,1)

    Why not use the code I gave you and change the start row to 20?
    Last edited by davesexcel; 04-13-2013 at 08:13 AM.

  13. #13
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    I finally got it working, BUT, when you save the file, close it, then reopen it... it fails to turn columns green when checked... how do you get it to save that functionality?

  14. #14
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    k.. i figured it out! Apparently, the code was tied to the test sheet that was open. So when I closed them all, the code was not on the saved "real" sheet... I had to redo it all, but it worked fine and worked fine after saving...

    Thanks a TON for all the help davesexcel!!!

  15. #15
    Registered User
    Join Date
    04-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    I have another question... how can i run a script to delete all the hundreds of boxes? I'm running into a problem where if I copy and paste the sheet into new tabs, the checkboxes can be messed up easily.... so it seems that it would be easier to delete them all then recreate them?

  16. #16
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: How to set up checkboxes on 250 rows that toggles the entire row color when clicked?

    I believe
        activesheet.checkboxes.delete
    Should work.

+ 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.6.0 RC 1