Chào các anh chị GPE,
Tôi có tìm kiếm trên mạng đoạn code để ngăn không cho user delete sheet trong workbook 'Task Cotrol'.
Code như sau:
Private Sub Worksheet_Activate()
Dim CB As CommandBar
Dim Ctrl As CommandBarControl
For Each CB In Application.CommandBars
Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
If Not Ctrl Is Nothing Then
Ctrl.OnAction = "RefuseToDelete"
Ctrl.State = msoButtonUp
End If
Next
End Sub
'-----------------------------------
Private Sub Worksheet_Deactivate()
Dim CB As CommandBar
Dim Ctrl As CommandBarControl
For Each CB In Application.CommandBars
Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
If Not Ctrl Is Nothing Then Ctrl.Delete
Next
End Sub
Public Sub RefuseToDelete()
MsgBox "This sheet can not be deleted!", _
Buttons:=vbExclamation, _
Title:="Deletion is limited | Task Reminder"
End Sub
Code hoạt động rất tốt. Nhưng tôi gặp một vấn đề sau đó: khi tôi đóng workbook 'Task Cotrol', và muốn delete sheet ở workbooks khác thì không thể thực hiện được. Nó sẽ hiện thông báo ở Sub RefuseToDelete và mở luôn workbook 'Task Cotrol'.
Vậy phải thêm đoạn code nào để làm sao để khôi phục lại trạng thái ban đầu khi đóng workbook 'Task Cotrol'?
Tôi có tìm kiếm trên mạng đoạn code để ngăn không cho user delete sheet trong workbook 'Task Cotrol'.
Code như sau:
Private Sub Worksheet_Activate()
Dim CB As CommandBar
Dim Ctrl As CommandBarControl
For Each CB In Application.CommandBars
Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
If Not Ctrl Is Nothing Then
Ctrl.OnAction = "RefuseToDelete"
Ctrl.State = msoButtonUp
End If
Next
End Sub
'-----------------------------------
Private Sub Worksheet_Deactivate()
Dim CB As CommandBar
Dim Ctrl As CommandBarControl
For Each CB In Application.CommandBars
Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
If Not Ctrl Is Nothing Then Ctrl.Delete
Next
End Sub
Public Sub RefuseToDelete()
MsgBox "This sheet can not be deleted!", _
Buttons:=vbExclamation, _
Title:="Deletion is limited | Task Reminder"
End Sub
Code hoạt động rất tốt. Nhưng tôi gặp một vấn đề sau đó: khi tôi đóng workbook 'Task Cotrol', và muốn delete sheet ở workbooks khác thì không thể thực hiện được. Nó sẽ hiện thông báo ở Sub RefuseToDelete và mở luôn workbook 'Task Cotrol'.
Vậy phải thêm đoạn code nào để làm sao để khôi phục lại trạng thái ban đầu khi đóng workbook 'Task Cotrol'?