+ Reply to Thread
Results 1 to 4 of 4

VBA Automatically Adding a Checkbox to List

Hybrid View

  1. #1
    Registered User
    Join Date
    11-04-2015
    Location
    London, england
    MS-Off Ver
    2007
    Posts
    60

    VBA Automatically Adding a Checkbox to List

    Hi, Can someone please help, i have the below command but i can not get it to add a check box every time i add to the list ...

    Private Sub CommandButton1_Click()
    Set ws = Worksheets("Sheet10")
    lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
    ws.Range("A" & lastrow).Offset(0, 1).Value = With ActiveSheet.CheckBoxes.Add(Selection.Left, Selection.Top, Selection.Width, Selection.Height)

    End Sub


    Also when the checkbox is ticked it should highlight the row in different color.
    Last edited by shelim481; 08-20-2018 at 07:47 AM.

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

    Re: VBA Automatically Adding a Checkbox to List

    This goes in a regular module, it will determine what range the checkbox is that you just clicked.
    You will have to play around with the colors...
    Sub RangeOfButtonOrShape()
        Dim r As Range, x
        Set r = ActiveSheet.Shapes(Application.Caller).TopLeftCell    'find the range of the button clicked.
        x = IIf(Range("Z" & r.Row) = False, xlNone, 16000 + r.Row * 100)
        Range("A" & r.Row & ":B" & r.Row).Interior.Color = x
    End Sub
    This will go into the worksheet module,
    It will add a checkbox into column B and create a linked cell to column Z, and assign the macro to the checkbox.

    Private Sub CommandButton1_Click()
        Dim ws As Worksheet, LastRow As Long, Rng As Range, c As Range, CkBx As CheckBox
    
        Set ws = Worksheets("Sheet10")
        With ws
            LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            Set Rng = .Range("B2:B" & LastRow)
            For Each c In Rng.Cells
                With c
                    Set CkBx = ws.CheckBoxes.Add _
                               (Top:=.Top, Width:=.Width, _
                                Height:=.Height, Left:=.Left)
                End With
                With CkBx
                    .Name = "cbx_" & c.Address(0, 0)
                    .Caption = c.Offset(, -1)
                  .LinkedCell = "Z" & c.Row
                    .OnAction = ThisWorkbook.Name & "!RangeOfButtonOrShape"
                End With
            Next c
    
    
        End With
    End Sub

  3. #3
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: VBA Automatically Adding a Checkbox to List

    Public Sub Test()`
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet3")
    lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
    
    
    For Each cell In ws.Range("A" & lastRow).Offset(0, 1)
    With ActiveSheet.CheckBoxes.Add(cell.Left, _
         cell.Top, cell.Width, cell.Height)
         .LinkedCell = cell.Offset(, 0).Address(External:=T)
         .Caption = Range(.LinkedCell).Row
        .Value = Range(.LinkedCell).Value
        .Placement = XlPlacement.xlMoveAndSize
    End With
    ws.Range("a" & lastRow).Value = lastRow
    Next
    End Sub
    Last edited by nigelog; 08-20-2018 at 10:33 AM.

  4. #4
    Registered User
    Join Date
    11-04-2015
    Location
    London, england
    MS-Off Ver
    2007
    Posts
    60

    Re: VBA Automatically Adding a Checkbox to List

    Thank YOU All

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Checkbox is true automatically
    By Sanjibghosh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2018, 10:12 AM
  2. Automatically adding names to list
    By HarleyRider in forum Excel General
    Replies: 7
    Last Post: 01-01-2018, 11:24 PM
  3. Automatically insert checkbox?
    By excelforbeginners in forum Excel General
    Replies: 0
    Last Post: 01-04-2012, 02:27 PM
  4. Deselecting A CheckBox Automatically
    By heliskier89 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-17-2011, 02:22 PM
  5. Excel 2008 : Adding a list of numbers automatically
    By MaximusPrimal in forum Excel General
    Replies: 0
    Last Post: 03-07-2011, 02:40 AM
  6. Automatically adding a row to a list
    By mayerc in forum Excel General
    Replies: 1
    Last Post: 07-10-2006, 04:35 PM
  7. Need checkbox automatically in some cells of a new row
    By stuff8458 in forum Excel General
    Replies: 0
    Last Post: 11-08-2005, 05:10 PM

Tags for this Thread

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