Quote Originally Posted by royUK View Post
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
Thank for reply Roy is that possible we can define the range in code so that macro will run on that range only instead on whole sheet