Automatically Lock an Excel Row When a Cell Is Filled
Question details
Automatically lock a worksheet row when a designated cell receives a value.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Users enter data in editable worksheet cells, and a completed row should no longer be editable once its designated cell is filled.
- Observed behavior
- A change in the designated cell should trigger worksheet protection for the corresponding row.
Use a desktop version of Excel that supports VBA, and decide which column will trigger the row lock. The example monitors column B; replace the sample password and adjust the range to match your worksheet.
Lock completed Excel rows with a Worksheet_Change macro
Use this method when entering a value in a designated cell should immediately make its entire row read-only.
The Locked setting takes effect only when the worksheet is protected. The example locks rows when cells in B2:B1000 contain nonblank values; it does not automatically unlock rows when cells are cleared.
The event responds to direct edits and pasted values, not to formula results changing during recalculation. It also does not lock rows that were already filled before the macro was installed.
Select the cells users may edit, press Ctrl+1 or open Format Cells, and clear Locked on the Protection tab. Leave cells that should always be protected locked.
Select Review > Protect Sheet, enter a password, and confirm it. Use the same password in the macro below.
Right-click the worksheet tab, select View Code, and paste this code into that worksheet's code module. Replace YourPassword with the password used to protect the sheet: Private Sub Worksheet_Change(ByVal Target As Range) Dim watched As Range Dim cell As Range Dim message As String Set watched = Intersect(Target, Me.Range("B2:B1000")) If watched Is Nothing Then Exit Sub On Error GoTo Failed Me.Unprotect Password:="YourPassword" For Each cell In watched.Cells If Not IsError(cell.Value2) Then If Len(Trim$(CStr(cell.Value2))) > 0 Then cell.EntireRow.Locked = True End If End If Next cell Me.Protect Password:="YourPassword" Exit Sub Failed: message = Err.Description On Error Resume Next Me.Protect Password:="YourPassword" On Error GoTo 0 MsgBox "The row lock could not be completed: " & message & _ ". Check the worksheet protection status.", vbExclamation End Sub
Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). Reopen the file, enable macros if you trust it, and enter a value in a monitored cell to verify that its row becomes read-only.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for editing and organizing workbooks. Its Excel format compatibility helps you move existing files; the Excel VBA row-locking macro should be tested separately because macro support varies by WPS edition and platform.

Frequently Asked Questions
Why is the row still editable after the designated cell is filled?
Check that the code is in the correct worksheet's code module, the edited cell is within B2:B1000, and macros are enabled. Also confirm that Review > Protect Sheet has been applied and that the password in the code matches.
Can I monitor a different column?
Yes. In the worksheet code, replace Me.Range("B2:B1000") with the range you want to monitor, such as Me.Range("D2:D1000").
Will a formula result trigger the row lock?
No. Worksheet_Change does not fire when a formula result changes during recalculation. That scenario requires a different approach, such as handling recalculation and checking the relevant cells.
How do I edit a row after it has been locked?
Select Review > Unprotect Sheet and enter the password. To make the row editable after protection is restored, select the appropriate cells, open Format Cells > Protection, and clear Locked before protecting the sheet again.
Does the macro lock rows that were already completed?
No. The example only checks monitored cells changed after the macro is installed and enabled. Existing completed rows must be locked separately or handled by an additional macro.




