The default for cells is locked, so first unlock all the cells.
Click the empty grey box between the colmn & row headers to select all cells -> Format -> Protection -> uncheck Locked. Then use this code
Option Explicit
Sub Button1_Click()
Const PW As String = "secret"
Dim rNonBlanks As Range
On Error Resume Next
Set rNonBlanks = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
On Error GoTo 0
rNonBlanks.Locked = True
ActiveSheet.Protect PW
End Sub
Bookmarks