Private Sub Worksheet_Change(ByVal T As Range)
If ActiveSheet.Shapes("REC_CheckBox").TextFrame.Characters.Text = "核取方塊 (已關閉)" Then Exit Sub
If T.Count > 1 Then Exit Sub ' 單選儲存格
Application.EnableEvents = False
If T.Offset(, -1) = "" And T <> "" Then
With T.Offset(, -1)
.Value = ChrW(163)
.Font.Name = "Wingdings 2"
.Font.Size = 30 ' 不核取
End With
With T ' 核取方塊內容
.Font.Name = "微軟正黑體"
.Font.Size = 12
End With
ElseIf (T.Offset(, -1) = ChrW(163) Or T.Offset(, -1) = ChrW(82)) And T = "" Then
' 將 核取方塊位置 和 時間戳記 刪除 !
With Range(T.Offset(, -1).Address & "," & T.Offset(, 1).Address)
.Value = ""
.Font.Name = "微軟正黑體"
End With
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)
If T(1, 2) = "" Then Exit Sub ' 抓核取方塊右邊的內容 ,為空不執行
If Not (T(1, 1) = ChrW(163) Or T(1, 1) = ChrW(82)) Then Exit Sub
Application.EnableEvents = False
If T.Value = ChrW(82) Then If MsgBox("是否取消勾選?", vbYesNo) = vbNo Then GoTo Handle_Cancel
With T
.Value = IIf(.Value = ChrW(163), ChrW(82), ChrW(163))
.Font.Name = "Wingdings 2"
.Font.Size = 30
End With
If T.Value = ChrW(82) And T(1, 3) = "" Then
With T(1, 3)
.Value = Now()
.Font.Name = "微軟正黑體"
.NumberFormatLocal = "yyyy/mm/dd hh:mm:ss"
End With
Columns(Split(T(1, 3).Address, "$")(1) & ":" & Split(T(1, 3).Address, "$")(1)).EntireColumn.AutoFit
ElseIf T.Value = ChrW(163) Then
T(1, 3) = ""
MsgBox "【 時間戳記 】 資料已刪除 !!"
T(1, 3).Select
Else
MsgBox "有資料 !! 【 時間戳記 】 無法記錄 !!"
T(1.3).Select
End If
Handle_Cancel:
Application.EnableEvents = True
Cancel = True
End Sub
Sub Turn_OnOff_CheckBox()
Dim REC_text As String
REC_text = ActiveSheet.Shapes("REC_CheckBox").TextFrame.Characters.Text
If REC_text = "核取方塊 (已關閉)" Then
ActiveSheet.Shapes("REC_CheckBox").TextFrame.Characters.Text = "核取方塊 (已開啟)"
ElseIf REC_text = "核取方塊 (已開啟)" Then
ActiveSheet.Shapes("REC_CheckBox").TextFrame.Characters.Text = "核取方塊 (已關閉)"
Else
ActiveSheet.Shapes("REC_CheckBox").TextFrame.Characters.Text = "核取方塊 (Error)"
End If
End Sub
Sheet1 工作表內的 REC_CheckBox 矩形,需指定巨集為 Module1 的「 Turn_OnOff_CheckBox 」。
下一篇 [VBA] 如何將「核取方塊」變大 ? (方法2),將使用 Excel 常見的文字框來設計成 核取方塊 樣式,敬請期待 ~
以上就是這次的分享,請持續關注我 和 Meiko微課頻道,謝謝大家 ~
有任何問題,請在下方留言討論,或加入 LINE社群 Meiko微課小綠群(粉絲交流群),歡迎您的加入。