我需要为链接到该单元格的每个单元格添加一个复选框。选中时将返回 true,未选中时将 false 返回到分配给它的单元格。
工作表有数千个单元格,当我手动插入它们时,我意识到必须有更好的解决方案。
给你,克林顿。
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
这是我用来向所有选定单元格添加居中复选框的通用 VBA 宏:
'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)