我正在尝试插入多个复选框(100+),但不想单独添加它们。有这么快的方法吗?我尝试了下拉方法,它确实做得更多,但它们都连接起来 - 复制和粘贴也是如此!单击1时,它们都是!
单击勾选框时触发的宏如下所示,我想知道它是否可以更改?
Sub tick()
Dim ws As Worksheet
Dim chk As CheckBox
Dim lColD As Long
Dim lColChk As Long
Dim lRow As Long
Dim rngD As Range
lColD = 3 'number of columns to the right for date
Set ws = ActiveSheet
Set chk = ws.CheckBoxes(Application.Caller)
lRow = chk.TopLeftCell.Row
lColChk = chk.TopLeftCell.Column
Set rngD = ws.Cells(lRow, lColChk + lColD)
Select Case chk.Value
Case 1 'box is checked
rngD.Value = Date
Case Else 'box is not checked
rngD.ClearContents
End Select
End Sub
答案 0 :(得分:2)
这是我经常使用的代码。如果您选择一个范围并运行此宏 - 它将为每个选定的单元格添加一个复选框,并将其绑定到该单元格值(布尔值)
我不使用字幕 - 但如果您愿意,可以轻松修改它。
我希望这有助于
Sub Add_checkboxes()
Application.ScreenUpdating = False
For Each the_cell In Selection
ActiveSheet.CheckBoxes.Add(Range(the_cell.Address).Left, Range(the_cell.Address).top, 18, 12).Select
With Selection
.Caption = ""
.Value = xlOff '
.LinkedCell = the_cell.Address
.Display3DShading = False
End With
Next
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:1)
将此代码添加到VBA中的Sheet-Module
,并将要用作复选框的列标记为“选择”(仅作为示例)。双击时,此过程将指定的单元格标记为“X”
Private Sub Worksheet_beforedoubleClick(ByVal Target As Range, Cancel As Boolean)
'If row 6 of a cell contains "Selection" all rows below are used as checkboxes.
If Me.Cells(6, Target.Column) = "Selection" And Target.Row > 6 Then
'Function to mark the field with an "x"
If Target.Value = "x" Then
Target.Value = ""
Else
Target.Value = "x"
End If
End If
End Sub