将checkbox插入每个单元格的脚本,并将其分配给Excel中的单元格

我正在一个项目中,我需要添加一个checkbox,以链接到该单元格的每个单元格。 单击时,将返回true,如果未选中,则将其分配给的单元格返回false。 工作表有成千上万的单元格,而且我正在手动插入它们,我意识到必须有一个更好的解决scheme。 我正在处理的工作表是这样的:

Excel工作表需要checkbox

请让我知道如果我应该运行一个脚本/macros或什么 – 我非常感谢您的帮助!

在这里,克林顿。

Sub AddCheckBoxes() Dim cb As CheckBox Dim myRange As Range, cel As Range Dim wks As Worksheet Set wks = Sheets("mySheet") 'adjust sheet to your needs Set myRange = wks.Range("A1:A10") ' adjust range to your needs For Each cel In myRange Set cb = wks.CheckBoxes.Add(cel.Left, cel.Top, 30, 6) 'you can adjust left, top, height, width to your needs With cb .Caption = "" .LinkedCell = cel.Address End With Next End Sub 

这里是更通用的VBAmacros,我使用添加中心checkbox到所有选定的单元格:

 'ActiveSheet.DrawingObjects.Delete ' optional to delete all shapes when testing Dim c As Range, cb As CheckBox For Each c In Selection Set cb = c.Worksheet.CheckBoxes.Add(c.Left + c.Width / 2 - 8.25, _ c.Top + c.Height / 2 - 8.25, 0, 0) ' 8.25 is cb.Height / 2 cb.Text = vbNullString ' to clear Caption cb.LinkedCell = c.Address(0, 0) ' Example A1 instead of $A$1 cb.Name = "cb" & cb.LinkedCell ' optional Next Selection.NumberFormat = ";;;" ' optional to hide the cell values Selection = True ' optional to check all at once (or 'selection = [#N/A]' for all xlMixed)