You lock a row in Excel with VBA by setting its Locked property to True, then turning on sheet protection — Rows(5).Locked = True followed by ActiveSheet.Protect. Nothing happens until protection is enabled, which is why so many people set Locked = True, see the cells still editable, and assume the code failed. Also remember that Excel locks every cell by default, so locking a single row usually means unlocking everything first and then locking just the rows you want.
This guide walks through the working code, explains the protection step, and covers the mutual-lock trick that lets a list stay editable while fixed header or total rows stay protected.
What You'll Need
- Excel on Windows or Mac with macros enabled (the Developer tab is available under File > Options > Customize Ribbon).
- A workbook saved as
.xlsm, because a standard.xlsxfile discards VBA code when closed. - A workbook that is not already protected, or the password used to unprotect it. You cannot change the locked status of cells on a protected sheet without unprotecting first.
- If you plan to distribute the file, a password you are willing to share or store safely. Forgotten sheet passwords can often be removed, but forgotten workbook-level passwords are harder to recover.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (on a Mac, Fn+Alt+F11 or use Developer > Visual Basic). This opens a separate editor window where your code lives.
- Insert a module. In the editor, click Insert > Module. A blank code window appears. Writing macros in a standard module makes them available from the Macro dialog; code in a worksheet module only reacts to events on that sheet.
- Unprotect the sheet if it is already protected. Add the first line of your macro as
ActiveSheet.Unprotect "yourpassword"— orActiveSheet.Unprotectif there is no password. Most “method failed” errors come from skipping this. - Unlock all cells so you start from a clean slate. Because every cell is locked by default, write
Cells.Locked = False. This makes the whole sheet editable once protection is re-applied. - Lock the row you want. Use
Rows(5).Locked = Truefor row 5, orRows("5:7").Locked = Truefor a range of rows. You can also targetRange("A5:Z5").Locked = Trueif you only want part of the row locked. TheLockedproperty is what protection reads, so it must be set before the sheet is protected — order matters. - Turn protection on. Add
ActiveSheet.Protect Password:="yourpassword". Without this line, the locked flag has no effect at all. You can addDrawingObjects:=FalseorAllowFiltering:=Trueas arguments if users need to filter or sort while locked. - Disable protection explicitly if you want to see the effect immediately. If you only want to lock a row without protecting the sheet at that moment, expose it deliberately:
Cells.Locked = False: Rows(1).EntireRow.Locked = Trueand then protect through the ribbon (Review > Protect Sheet) so you can inspect the result in the UI. - Run the macro. Place the cursor inside the procedure and press F5, or go back to Excel and use Developer > Macros. Try typing in the locked row — Excel should refuse the edit and show a protection message.
- Save the file as a macro-enabled workbook. File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm). Saving as
.xlsxtriggers a warning that macros will be removed.
Common Mistakes
Forgetting protection. The locked flag is only a permission marker; it does nothing while the sheet is unprotected. If your row is still editable, you almost certainly never called Protect.
Locking a row on an already protected sheet. Excel raises an error when you change Locked while protection is active. Unprotect first, change the flags, then protect again.
Assuming only your row is locked. New sheets start with every cell locked. Without the Cells.Locked = False step, protecting the sheet locks the whole worksheet, not just row 5.
Locking the wrong thing. Rows(5).Locked = True sets the flag on every cell in row 5, but if you set Rows(5).EntireRow.Select thinking that locks it, you have only selected it. Selection and locking are unrelated.
Losing the password. Hard-coding a password in plain text inside the macro is readable by anyone who opens the VBA editor. If that matters, prompt the user for it with InputBox instead.
Protecting without allowing what people need. A protected sheet blocks sorting, filtering, formatting and inserting rows by default. Users then complain the sheet is “broken” when it is behaving exactly as configured.
Tips
A handy variant locks all rows except the ones that match a condition — for example keeping a data-entry range open while locking totals. Loop the used range and set Locked per row based on a cell value. This is the usual way to keep a “Form” style sheet usable.
If you need rows locked only for some users, combine protection with a check on Environ("USERNAME") or a named range holding authorised names, and skip the Protect call for those users. Note that this is convenience, not security — anyone who can open the VBA editor can read or change the code, so password-protect the VBA project (Tools > VBAProject Properties > Protection) if that matters.
Store the password in a hidden sheet or a defined name rather than in the code string if you want to change it without editing the macro. Keep a note of it outside the workbook.
Use ActiveSheet.Unprotect at the start and Protect at the end even when testing, so re-running the macro never fails because the sheet was left protected from the last run.
For repeated use, wrap the row number in a variable so the same macro works for any row: Sub LockRow(r As Long) with Rows(r).Locked = True, called as LockRow 5.
Troubleshooting
The row is still editable. Check that Protect actually ran. Add a message box after it, or look at Review > Protect Sheet — if it says “Unprotect Sheet”, the sheet is protected, and the problem is that your row was not locked before protection.
“Unable to set the Locked property of the Range class.” The sheet is protected. Unprotect it before changing the property.
Everything is locked, not just the row. You skipped Cells.Locked = False, or you ran the macro on the wrong sheet. Qualify the sheet explicitly: Worksheets("Sheet1").Cells.Locked = False.
Nothing happens when running from the Macro dialog. The macro may be in a worksheet module rather than a standard module, or the workbook is open in a protected view. Close and reopen from a trusted location.
The macro works once then errors on the second run. The sheet was left protected. Make the unprotect/protect pair symmetrical.
Users cannot sort or filter. Re-run Protect with AllowFiltering:=True and AllowSorting:=True, or unlock the header row rather than locking every row.
The code vanished after saving. The file was saved as .xlsx. Save as .xlsm.
Frequently Asked Questions
Does Locked = True lock the row immediately?
No. It only sets the permission flag. The row becomes non-editable when the sheet is protected with Protect or the Review tab.
How do I lock a row but leave the rest editable?
Unprotect the sheet, set Cells.Locked = False for the whole sheet, set Rows(n).Locked = True for the row, then protect again.
Can I lock rows without a password?
Yes. Call ActiveSheet.Protect with no password argument. Anyone can then unprotect from the ribbon, so it prevents accidents, not deliberate changes.
Does locking a row stop people deleting it?
Yes, once the sheet is protected. Deleting a locked row is refused, and inserting rows is also blocked unless you allow it in the protection options.
Why does my locked row still allow formatting changes?
Protection allows formatting of unlocked cells by default, and some options are enabled when you protect. Call Protect with the format arguments set to False if you want formatting blocked on locked cells.
Can I lock a row on a password-protected sheet from VBA?
Only if you supply the password in the Unprotect call. There is no supported way to bypass a password you do not know.