Following the query raised here: VBA Excel spellchecking for protected workbook
I would like to make my workbook protected in the same shape as it was at the beginning.
Basically most of my sheets are protected from row 1 to 5.
It has been marked as the Private_Sub for each sheet separately as follows:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const pw As String = "Secret"
Dim rFormulaCheck As Range
On Error Resume Next
With Target
.Parent.Unprotect pw
.Locked = False
.FormulaHidden = False
If .Cells.Count = 1 Then
If .HasFormula Then
.Locked = True
.FormulaHidden = True
.Parent.Protect pw, , , , 1
End If
ElseIf .Count > 1 And .Count < 5 Then
With .SpecialCells(xlCellTypeFormulas)
.Locked = True
.FormulaHidden = True
.Parent.Protect pw, , , , 1
End With
End If
End With
On Error GoTo 0
End Sub
As you can see, the password has been set as "Secret" and it covers only the first 5 rows.
In terms of the Spellchecking I have to unlock them, as per the code below:
Sub Spellcheck()
For Each sh In Worksheets
sh.Unprotect "Secret"
sh.CheckSpelling
sh.Protect "Secret"
Next sh
End Sub
However, when I use sh.Protect "Secret"
. I am unable to edit any cell throughout the document.
It's expressed by error:
The cell or chart you're trying to change is on a protected sheet. To make a change, unprotect the sheet. You might be requested to enter a password.
So effectively so far I must switch off the sh.Protect "Secret" if I want to make my document editable. Unfortunately the Private Sub Worksheet_SelectionChange(ByVal Target As Range)
doesn't work anymore.
Is there a chance to bring these protection settings back to the initial stage after the spellchecking process?
UPDATE:
I was trying to do something like this:
Sub Spellcheck()
For Each sh In Worksheets
sh.Unprotect "Secret"
sh.CheckSpelling
'sh.Protect "Secret"
Next sh
Call Worksheet_SelectionChange
End Sub
But now I am getting error: Sub or function not defined.
It's not defined in module, but it's defined in the most of the sheets.