Add Checkboxes through VBA code

tazz

Board Regular
Joined
Jul 7, 2004
Messages
86
Hi,

I'm trying to add a series of checkboxes (Excel not activeX) to a worksheet. I'd like to have code that would create the check box and associate it with a cell based on a condition. So, if a cell in Column D has a value (or is not blank) I'd like to have a check box created in Column C and the checkbox be associated to the cell (D2 has a value, check box is created in C2 and C2 now displays True/False base on check box value). I can do this the old fashioned way, but I add 30 - 50 users at a time and that's a long tedious process.

Any thoughts?

Regards,

Tazz
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
This is the sort of thing. Need to tweak a bit because checkbox control overall size is bigger than the actual box. You might also be interested in my alternative.
Code:
Sub test()
    Dim ToRow As Long
    Dim LastRow As Long
    Dim MyLeft As Double
    Dim MyTop As Double
    Dim MyHeight As Double
    Dim MyWidth As Double
    '--------------------------
    LastRow = Range("D65536").End(xlUp).Row
    For ToRow = 2 To LastRow
        If Not IsEmpty(Cells(ToRow, "D")) Then
            '-
            MyLeft = Cells(ToRow, "C").Left
            MyTop = Cells(ToRow, "C").Top
            MyHeight = Cells(ToRow, "C").Height
            MyWidth = MyHeight = Cells(ToRow, "C").Width
            '-
            ActiveSheet.CheckBoxes.Add(MyLeft, MyTop, MyWidth, MyHeight).Select
             With Selection
                .Caption = ""
                .Value = xlOff
                .LinkedCell = "C" & ToRow
                .Display3DShading = False
            End With
        End If
    Next
End Sub

http://www.mrexcel.com/board2/viewtopic.php?t=154562&highlight=
 
Upvote 0
Properties Window is not available...`

I tried this as well and it works beautifully. My only question is how do I modify the properties of the checkboxes created? I try clicking on one to show the properties but it only check the box.

Thnaks

Lamar
 
Upvote 0
Properties Window is not available...`

I tried this as well and it works beautifully. My only question is how do I modify the properties of the checkboxes created? I try clicking on one to show the properties but it only check the box.

Thnaks

Lamar
 
Upvote 0
Lamar,

I think if you click on the design button you can then see the checkbox properties.

HTH,
Tazz
 
Upvote 0
Right click the control and Format Control.
If you record a macro of your setting you can incorporate it into the overall thing like this :-
Code:
Sub test()
    Dim ToRow As Long
    Dim LastRow As Long
    Dim MyLeft As Double
    Dim MyTop As Double
    Dim MyHeight As Double
    Dim MyWidth As Double
    '--------------------------
    LastRow = Range("D65536").End(xlUp).Row
    For ToRow = 2 To LastRow
        If Not IsEmpty(Cells(ToRow, "D")) Then
            '-
            MyLeft = Cells(ToRow, "C").Left
            MyTop = Cells(ToRow, "C").Top
            MyHeight = Cells(ToRow, "C").Height
            MyWidth = MyHeight = Cells(ToRow, "C").Width
            '-
            ActiveSheet.CheckBoxes.Add(MyLeft, MyTop, MyWidth, MyHeight).Select
             With Selection
                .Caption = ""
                .Value = xlOff
                .LinkedCell = "C" & ToRow
                .Display3DShading = False
                '------------------------------------------------------
                '- format
                .Placement = xlFreeFloating
                .PrintObject = True
                '-------------------------------------------------------
                .ShapeRange.Fill.Visible = msoTrue
                .ShapeRange.Fill.Solid
                .ShapeRange.Fill.ForeColor.SchemeColor = 13
                .ShapeRange.Fill.Transparency = 0#
                .ShapeRange.Line.Weight = 0#
                .ShapeRange.Line.DashStyle = msoLineSolid
                .ShapeRange.Line.Style = msoLineSingle
                .ShapeRange.Line.Transparency = 0#
                .ShapeRange.Line.Visible = msoTrue
                .ShapeRange.Line.ForeColor.SchemeColor = 64
                .ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
                '-------------------------------------------------------------
            End With
        End If
    Next
End Sub
 
Upvote 0
Im working on a similar problem...Except if you look at my sheet, I have included a link to download it here:
http://www.aiimcalgary.org/newtest.xls
I have command buttons that can create word documents.
(Its in Column Y)

However within the body of the Word Document is a list of file numbers which comes from column Q.

I would like to preform a similar task, have a checkbox in every cell in column Q that has data...That way users can select which file numbers they want, then can scroll over to the command button "Create Transmittal" and once they select the command button those file numbers checked, will be listed in the body of the word document.

Is that possible to do?
 
Upvote 0
This worked great, now to add to this. How do I add a date stamp to column I when the checkbox is clicked? Appreciate the help!

-tom
 
Upvote 0
Hi guys,

I am new to this, however, I am also interested in above topic. I copy paste the above code from BrianB in VB and then run it, but I didn't get any output in my excel. Am I doing in the right way? I appreciate your advise. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

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