How to Lock the data in a cell in excel using vba

calendar_today Asked Jun 14, 2010
thumb_up 18 upvotes
history Updated April 16, 2026

Question posted 2010 · +9 upvotes

I want to stop others from editing the cell contents in my excel sheet using VBA. Is it possible to do this?

Accepted answer +18 upvotes

You can first choose which cells you don’t want to be protected (to be user-editable) by setting the Locked status of them to False:

Worksheets("Sheet1").Range("B2:C3").Locked = False

Then, you can protect the sheet, and all the other cells will be protected. The code to do this, and still allow your VBA code to modify the cells is:

Worksheets("Sheet1").Protect UserInterfaceOnly:=True

or

Call Worksheets("Sheet1").Protect(UserInterfaceOnly:=True)

3 code variants in this answer

  • Variant 1 — 1 lines, starts with Worksheets("Sheet1").Range("B2:C3").Locked = False
  • Variant 2 — 1 lines, starts with Worksheets("Sheet1").Protect UserInterfaceOnly:=True
  • Variant 3 — 1 lines, starts with Call Worksheets("Sheet1").Protect(UserInterfaceOnly:=True)

Top excel-vba Q&A (6)

+18 upvotes ranks this answer #21 out of 136 excel-vba solutions on this site — top 15%.