Macro code that generates a checkbox when a command button is clicked

benhop

New Member
Joined
Nov 18, 2008
Messages
35
I want to write a macro that will create a checkbox in
a cell that meets certain conditions when I click a command button on the top of a column. For example, if the cell's value falls between 1 and -1 in the Q column, the same Q column in which the command button lies, then a macro would generate a checkbox once the Q command button was clicked by the user. I have sample code that works as a macro only when I activate a cell that meets the <1 and >-1 conditions. Ideally, I want the macro to create a checkbox when the user clicks a command button, even if those cells who meet the <1 and >-1 conditions aren't activated by the user. Thanks for any help. Here is my code so far:

Code:
Sub Mycheckbox2()
Dim objCBox As Object, c As Object
For Each c In Selection
If (c.Value <= 1 And c.Value >= -1) Then
c.Select
Set objCBox = ActiveSheet.CheckBoxes.Add _
(Left:=ActiveCell.Left, Top:=ActiveCell.Top, _
Width:=20, Height:=ActiveCell.Height)
End If
Next c
End Sub
 
You could build on the code that you have.

This example looks at all of your values in Q1 to the Last row that has data in column Q.

Code:
Sub Mycheckbox2()
Dim objCBox As Object
Dim c As Range
Dim lRow As Long

lRow = Cells(Rows.Count, "Q").End(xlUp).Row
For Each c In Range("Q1:Q" & lRow)
    If (c.Value <= 1 And c.Value >= -1) Then
        c.Select
        Set objCBox = ActiveSheet.CheckBoxes.Add _
            (Left:=ActiveCell.Left, Top:=ActiveCell.Top, _
            Width:=20, Height:=ActiveCell.Height)
    End If
Next c
End Sub
 
Last edited:
Upvote 0

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