Forum Discussion
_hgr_excelhelp
Jan 16, 2020Copper Contributor
Format ALL checkboxes to link to adjacent cells w/o having to Format Control all individually?
There are 96 spreadsheets. Each spreadsheet has 60 checkboxes. I need to find all of the boxes that were checked and calculate various things from there. But first step's first, link each checkbox wi...
- Jan 16, 2020
My example sized the checkboxes at 30 pixels high, starting at (0,0) i.e. top left of the sheet, and with a row height of 30 as well. Play around a little with the parameters to make it match your requirements.
HasarUndusu
Sep 02, 2024Copper Contributor
try this code
Sub CreateCheckboxes()
Dim cell As Range
Dim chkBox As CheckBox
Dim ws As Worksheet
Set ws = ActiveSheet
For Each cell In Selection
' Calculate the position of the checkbox
Dim leftPosition As Double
Dim topPosition As Double
Dim chkWidth As Double
Dim chkHeight As Double
chkWidth = 15
chkHeight = cell.Height / 2
leftPosition = cell.Left + cell.Width / 2 - chkWidth / 2
topPosition = cell.Top + (cell.Height / 2) - (chkHeight / 2)
' Add checkbox and set its properties
Set chkBox = ws.CheckBoxes.Add(leftPosition, topPosition, chkWidth, chkHeight)
With chkBox
.Caption = ""
.Value = xlOff
.LinkedCell = cell.Offset(0, 0).Address
.Display3DShading = False
End With
' Hide the value in the linked cell
cell.Offset(0, 0).Value = "False"
cell.Offset(0, 0).NumberFormat = ";;;"
Next cell
End Sub
you can see the result, the cells you select is added with checkboxes, and the checkboxes is linked to each of the cells, and the value(it should be "true" or "false") of the cells is hide.
the result
linked to the original cells, and the value of cells is hide