在VBA中创buildcheckbox,设置“单元格移动和大小”
我已经创build了一个电子表格,里面有很多checkbox。 有时候我已经写完了一个专栏,希望把它从视图中隐藏起来。
但是,如果我隐藏列,它不会隐藏该列中的checkbox。
如果手动将checkbox属性更改为“使用单元格移动和大小”,则会解决此问题。 但正如我所说,有很多checkbox,我用macros创build它们。
所以我试图添加下面的创buildmacros的vba:CBX.Placement = xlMoveAndSize
但是这并没有改变。
有任何想法吗?
这里是完整的VBA:
Sub CellCheckboxReview() Dim myCell As Range Dim myRng As Range Dim CBX As CheckBox With ActiveSheet 'remove comment if you want to delete all .CheckBoxes.Delete Set myRng = .Range(ActiveCell.Offset(19, 0), ActiveCell.Offset(23, 0)) End With For Each myCell In myRng.Cells With myCell Set CBX = .Parent.CheckBoxes.Add _ (Top:=.Top, _ Left:=.Left, _ Width:=.Width, _ Height:=.Height) CBX.Name = "Checkbox_" & .Address(0, 0) CBX.Caption = "" 'or what you want CBX.Value = xlOff CBX.LinkedCell = .Address(external:=True) CBX.Placement = xlMoveAndSize .NumberFormat = ";;;" End With Next myCell
结束小组
xlMoveAndSize
选项xlMoveAndSize
用于单独的checkbox,即使在Excel的GUI中也是如此。 但是,如果您将checkboxGroup
,则可以应用它。
Sub CellCheckboxReview() Dim myCell As Range, myRng As Range, CBX As CheckBox Set myRng = ActiveSheet.Range(ActiveCell.Offset(19, 0), ActiveCell.Offset(23, 0)) Dim ar(1 To 5) As String ' <-- an array to be used to group the checkboxes Dim i As Long: i = 1 For Each myCell In myRng.Cells With myCell Set CBX = .Parent.CheckBoxes.Add _ (Top:=.Top, _ Left:=.Left, _ Width:=.Width, _ Height:=.Height) CBX.Name = "Checkbox_" & .Address(0, 0) CBX.Caption = "" 'or what you want CBX.value = xlOff CBX.LinkedCell = .Address(external:=True) 'CBX.Placement = xlMoveAndSize ' <-- this has no effect for an individual checkbox .NumberFormat = ";;;" ar(i) = CBX.Name ' <-- add the shape's name to the array, for grouping later i = i + 1 End With Next myCell ' now group the checkboxes then set the desired placement With ActiveSheet.Shapes.Range(ar) .Group .Item(1).Placement = xlMoveAndSize End With End Sub