Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VBA Sort Locked Cells: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You cannot sort a range that contains locked cells while the worksheet is protected — Excel throws the error "The cell or chart you're trying to change is on a protected sheet." The fix is to temporarily unprotect the sheet, sort, then re-protect it, all inside one VBA routine. The shortest working version looks like this:

ActiveSheet.Unprotect Password:="yourpass"
Range("A1:D20").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
ActiveSheet.Protect Password:="yourpass"

That is the whole trick. Everything below explains how to make it reliable, how to keep locked cells locked, and what to do when Excel still refuses to sort.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm (macro-enabled), or you will lose the code on save.
  • The password for the sheet protection, if one is set. If the sheet was protected without a password, use Unprotect with no argument.
  • A clear idea of which column is the sort key and whether the range has a header row.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 on Windows, or Option+F11 on Mac. In the Project pane, right-click your workbook and choose Insert > Module.
  2. Write the unprotect line. Type ActiveSheet.Unprotect Password:="yourpass", replacing yourpass with the real password. If there is no password, just write ActiveSheet.Unprotect. Note that sheet protection passwords are case-sensitive.
  3. Turn off any user-interface blocking. If the workbook uses Application.DisplayAlerts or ScreenUpdating settings elsewhere, leave them alone here — the sort does not need them. But do make sure the sheet is not also protected at the workbook level (Review > Protect Workbook). If it is, unprotect the workbook first with ThisWorkbook.Unprotect Password:="yourpass".
  4. Define the range explicitly. Use a fixed range such as Range("A1:D20") or a dynamic one such as Range("A1").CurrentRegion. Avoid Cells or whole columns, because locked header or footer cells outside the data will still block the sort.
  5. Add the Sort method. Example: Range("A1:D20").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes. Change Key1 to the first cell of the column you want to sort by, and set Header:=xlNo if row 1 is data, not a heading.
  6. Re-protect the sheet. Add ActiveSheet.Protect Password:="yourpass". If the original protection had specific options (for example, allowing users to select locked cells but not format them), repeat those arguments here, or the sheet will come back with default protection and users will notice the difference.
  7. Run the macro. Press F5 with the cursor inside the routine, or assign it to a button (Developer > Insert > Button Form Control). Test on a copy of the workbook first.
  8. Verify the locked cells are still locked. After the run, click a locked cell — Excel should refuse to edit it. If it does not, your Protect line is missing or used the wrong password.

Menu labels and dialog wording can differ slightly by Excel version, device and region, but the VBA object model is the same on Windows and Mac.

Common Mistakes

  • Forgetting to re-protect. The macro runs, the sort works, and the sheet is left wide open. Always pair Unprotect with Protect in the same routine.
  • Sorting a range that includes locked cells outside the data. Even with the sheet unprotected, a sort range that overlaps merged or locked header cells can fail. Keep the range tight.
  • Using the wrong password case. "Pass" and "pass" are different. A failed Unprotect raises run-time error 1004.
  • Assuming ActiveSheet is the right sheet. If the user has another sheet selected, the macro unprotects and sorts the wrong one. Use Worksheets("Data") instead.
  • Protecting with default options. The default Protect call disallows selecting locked cells in some configurations. If the original sheet allowed selection, pass UserInterfaceOnly:=True or the matching Allow... arguments.

Tips

  • Wrap the whole routine in On Error GoTo Cleanup and put the Protect line in the cleanup block, so the sheet is re-protected even if the sort fails.
  • Use UserInterfaceOnly:=True when protecting: it lets VBA keep working on locked cells without unprotecting again, though it resets when the workbook is reopened.
  • Store the password in a hidden worksheet or a named constant rather than hard-coding it in several places.
  • If the sort key column contains blanks, decide in advance whether blanks go first or last — the Sort method has no direct option for this, so you may need to sort twice or use a helper column.
  • Test with a copy. Sorting is destructive and there is no undo once the macro finishes.

Troubleshooting

Run-time error 1004: "The cell or chart you're trying to change is on a protected sheet." The sheet is still protected when the sort runs. Check that the Unprotect line executes before the Sort line, and that you are unprotecting the same sheet you are sorting.

The macro runs but nothing moves. The Key1 range may point to a column outside the sort range, or Header:=xlYes may be treating your first data row as a heading. Set Header:=xlNo and retest.

Locked cells become editable after the run. The Protect line is missing, or it used a different password than the original. Re-run with the correct password.

Error 1004 on Unprotect itself. The password is wrong or the sheet is not protected. Check by hand: Review > Unprotect Sheet. If that prompts for a password, your macro must supply the same one.

The sort works in the editor but not from a button. The button may be assigned to a different macro, or the workbook may be opening in Protected View. Enable editing first.

If that did not work, step through the code line by line with F8 to see exactly which line fails, and check the sheet name in the Project pane matches the one in your code.

Frequently Asked Questions

Can I sort locked cells without unprotecting the sheet?

No. Excel blocks any change to a protected sheet, including sorting, regardless of whether the individual cells are locked. Unprotect, sort, re-protect.

Does the macro need the password?

Only if one was set. If the sheet was protected without a password, Unprotect with no argument works, and Protect with no argument re-protects it.

Will re-protecting reset the allowed actions?

Yes, unless you repeat the original Allow... arguments. Note the settings you used the first time and copy them into the Protect line.

Does this work on Mac?

Yes. The VBA object model is the same; only the keyboard shortcut to open the editor differs (Option+F11).

Why does the sort include my header row?

Because Header:=xlYes was set when row 1 is data, or the range starts above the header. Adjust the range or the Header argument.

Can I protect only some cells and leave others sortable?

No — protection applies to the whole sheet. Unlock the cells you want users to edit (Format Cells > Protection), but sorting still requires the sheet to be unprotected.