How to Lock Cells With Formulas and Protect Sheet using VBA

Опубликовано: 11 Июль 2026
на канале: Learn In 5 Minutes
1,137
3

In this tutorial you will learn how to lock cells with formulas and protect sheet using VBA macro programming in Microsoft Excel

VBA Code Without Password:
'This macro code will lock all the cells with formulas
Sub LockFormulasProtectSheet()
With ActiveSheet
.Unprotect
.Cells.Locked = False
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
.Protect AllowDeletingRows:=True
End With
MsgBox ("Formulas are locked successfully")
End Sub

VBA Code With Password:
'This macro code will lock all the cells with formulas
Sub LockFormulasProtectSheet()
Dim password As String
password = "Pass123"
With ActiveSheet
.Unprotect password:=password
.Cells.Locked = False
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
.Protect password:=password, AllowDeletingRows:=True
End With
MsgBox ("Formulas are locked successfully")
End Sub