Add checkboxes based on table column

Ladydee
Ladydee used Ask the Experts™
on
I had this question after viewing Checkbox - copy row to sheet.

My question is how to add checkboxes based on all value in table column with only one click on the button. below code only add one checkbox. thank you for help.

Sub NewCheckbox()
Dim lRow As Long
Dim target As Range
Dim obj As Object
'Adds a Forms toolbar checkbox to the cell
'Checkbox value is linked to that same cell, using a white font color
    
With Worksheets("Input")
    lRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row       'find  first empty row in database
    Set target = .Cells(lRow, 1)
    With .CheckBoxes.Add(target.Left, target.Top + 1, target.Width, target.Height - 1)
            .Caption = "Fakturer"
            .LinkedCell = target.Address
            .Name = "cb_" & target.Address(False, False)     'Name it like "cb_A11"
            .OnAction = "CheckboxClicked"
    End With
    target.Font.ColorIndex = 2     'White font color for linked cell TRUE/FALSE value
    target.Value = False
End With
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Can you please post a sample that shows where you want to add the checkboxes?
Roy CoxGroup Finance Manager

Commented:
I would use Marlett or Wingding Fonts to achieve the effect of CheckBoxes, see this example
NotCheckBoxes.xlsm

Author

Commented:
below is the example where I want to add the checkboxes. with only one button click, i want checkboxes added to all row based on table row.

Capture.PNG
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Sub NewCheckbox()
Dim lRow As Long
Dim target As Range
Dim obj As Object
'Adds a Forms toolbar checkbox to the cell
'Checkbox value is linked to that same cell, using a white font color
    
With Worksheets("Input")
    For lRow = .ListObjects(1).DataBodyRange.Row To .ListObjects(1).DataBodyRange.Rows.Count + _
                                                    .ListObjects(1).DataBodyRange.Row - 1
        Set target = .Cells(lRow, 1)
        With .CheckBoxes.Add(target.Left, target.Top + 1, target.Width, target.Height - 1)
                .Caption = "Fakturer"
                .LinkedCell = target.Address
                .Name = "cb_" & target.Address(False, False)     'Name it like "cb_A11"
                .OnAction = "CheckboxClicked"
        End With
        target.Font.ColorIndex = 2     'White font color for linked cell TRUE/FALSE value
        target.Value = False
    Next
End With
End Sub

Open in new window

Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
If you know the name of your name (for example MyTable), then this would be better.
Sub NewCheckbox()
Dim lRow As Long
Dim target As Range
Dim obj As Object
'Adds a Forms toolbar checkbox to the cell
'Checkbox value is linked to that same cell, using a white font color
    
With Worksheets("Input")
    For lRow = .ListObjects("MyTable").DataBodyRange.Row To .ListObjects("MyTable").DataBodyRange.Rows.Count + _
                                                    .ListObjects("MyTable").DataBodyRange.Row - 1
        Set target = .Cells(lRow, 1)
        With .CheckBoxes.Add(target.Left, target.Top + 1, target.Width, target.Height - 1)
                .Caption = "Fakturer"
                .LinkedCell = target.Address
                .Name = "cb_" & target.Address(False, False)     'Name it like "cb_A11"
                .OnAction = "CheckboxClicked"
        End With
        target.Font.ColorIndex = 2     'White font color for linked cell TRUE/FALSE value
        target.Value = False
    Next
End With
End Sub

Open in new window

Author

Commented:
how about i want to use it for all table in multiple worksheet?
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I believe I've answered the question you asked, so please close this question and ask a new question.
Roy CoxGroup Finance Manager
Commented:
With VBA you would need to run the code everytime a new row is added. With my suggestion the "checkboc2 would be automatic.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2016
Roy CoxGroup Finance Manager

Commented:
Pleased to help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial