Hello!
I have a strange issue.
On my worksheet, I initiate macros with added right-click buttons.
The macro first unlocks the sheet, changes the cells' (carried via parameter) content (clears), and locks it again.
During the subroutine's step-by-step debug everything works fine.
However, when initiated with a right-click, the sheet remains protected, and no error message at Unprotect (only on content change).
Code on Sheet1:
Code in Module1:
My only guess is that I cannot initiate sheet protect-unprotect from Module1.
I have a strange issue.
On my worksheet, I initiate macros with added right-click buttons.
The macro first unlocks the sheet, changes the cells' (carried via parameter) content (clears), and locks it again.
During the subroutine's step-by-step debug everything works fine.
However, when initiated with a right-click, the sheet remains protected, and no error message at Unprotect (only on content change).
Code on Sheet1:
VBA Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim MySubMenu As CommandBarControl
Dim cmdButton As CommandBarButton
Dim ContextMenu As CommandBar
Dim ctrl As CommandBarControl
'Set ContextMenu to the Cell menu
Set ContextMenu = Application.CommandBars("Cell")
'Delete custom controls
For Each ctrl In ContextMenu.Controls
ctrl.Visible = False
If ctrl.ID = 22 Or ctrl.ID = 21437 Then
ctrl.Delete
ElseIf ctrl.Tag = "CustomScheduleMenu" Then
ctrl.Delete
End If
Err.Clear
Next ctrl
If (ActiveSheet.Name = "Sheet1") And (Not (Intersect(Target, Range("c5:i62")) Is Nothing)) Then
'Clear all button
Set cmdButton = Application.CommandBars("Cell").Controls.Add(Type:=msoControlButton, before:=1, Temporary:=True)
With cmdButton
.OnAction = "=ClearAll(""" & Target.Address & """)"
.Caption = "Clear cell content"
.Style = msoButtonCaption
.Tag = "CustomScheduleMenu"
End With
End If
End Sub
Code in Module1:
VBA Code:
Public Sub ClearAll(ByVal cTarget As String)
Dim Target As Range
For Each Target In Sheets("Sheet1").Range(cTarget)
If (Target.Column >= 3 And Target.Column <= 9) And (Target.Row >= 5 And Target.Row <= 62) Then
Sheets("Sheet1").Unprotect Password:="whatever"
Target.Value = ""
Sheets("Sheet1").Protect Password:="whatever"
End If
Next
End Sub
My only guess is that I cannot initiate sheet protect-unprotect from Module1.