I have a worksheet I am developing that will eventually have about 3,000 checkboxes. I found a macro posted online to create the checkboxes, which works great. I also have a macro which will run an individual checkbox to do the following to the adjacent cell if the checkbox is clicked: change the cell value, lock the cell, and format the cell. Both sets of code work great. The problem is I will have to create 3,000 macros with 3,000 different names and open 3,000 checkboxes to assign the appropriate macro. I want to assign one macro to every checkbox as it is created that assigns a universal macro which simply says, if this checkbox is clicked, then make changes to the adjacent cell. I added an .onAction command to the below insertCheckboxes code to assign a macro called CheckBoxUniversal_Click. That works, now I need to create the code for CheckBoxUniversal_Click which will tell excel to make changes to the adjacent cell. Thoughts?
Sub insertCheckboxes()
Dim myBox As CheckBox
Dim myCell As Range
Dim cellRange As String
Dim cboxLabel As String
Dim linkedColumn As String
cellRange = InputBox(Prompt:="Cell Range", _
Title:="Cell Range")
linkedColumn = InputBox(Prompt:="Linked Column", _
Title:="Linked Column")
cboxLabel = InputBox(Prompt:="Checkbox Label", _
Title:="Checkbox Label")
With ActiveSheet
For Each myCell In .Range(cellRange).Cells
With myCell
Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _
Width:=.Width, Left:=.Left, Height:=.Height)
With myBox
.LinkedCell = linkedColumn & myCell.Row
.Caption = cboxLabel
.Name = "checkbox_" & myCell.Address(0, 0)
.onAction = "CheckBoxUniversal_Click"
End With
.NumberFormat = ";;;"
End With
Next myCell
End With
End Sub
Bookmarks