我在包含值(日期)的单元格中的Excel电子表格中有复选框。为了避免管理员在单击单元格时意外地检查框,我想调整复选框,以便必须双击才能注册勾选。我不想保护工作表,因为有不止一个人在不断编辑信息。有什么想法吗?
答案 0 :(得分:0)
请试试这个系统。安装在您有复选框的工作表的代码表中。
Option Explicit
Dim ChkVal() As Boolean
Dim EnableChk As Boolean
Private Sub Worksheet_Activate()
' 27 Oct 2017
Dim Shp As Shape
Dim Sn As String
ReDim ChkVal(Me.Shapes.Count)
For Each Shp In Me.Shapes
With Shp
If .Type = 12 And InStr(1, .Name, "checkbox", vbTextCompare) = 1 Then
ChkVal(Val(Mid(.Name, 9))) = .OLEFormat.Object.Object.Value
End If
End With
Next Shp
End Sub
Private Sub CbxChange(Cbx As MSForms.CheckBox)
' 27 Oct 2017
Dim Id As Integer
On Error GoTo ErrHandler
With Cbx
Id = Val(Mid(.Name, 9))
If Not EnableChk Then
Do Until .Value = ChkVal(Id)
.Value = Not .Value
Loop
Else
ChkVal(Id) = .Value
End If
End With
EnableChk = False
Exit Sub
ErrHandler:
If Err = 9 Then
Worksheet_Activate
Resume 0
Else
MsgBox "A programming error interrupted this procedure.", _
vbInformation, "Unidentified error"
End If
End Sub
Private Sub CheckBox1_Click()
CbxChange CheckBox1
End Sub
Private Sub CheckBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
EnableChk = True
End Sub
对于工作表上的每个复选框,您将需要一对事件过程,如最后两个。我试着快速地做到这一点。