how to lock row in excel with code examples

Excel is a powerful tool that allows users to manage and analyze a large amount of data effectively. However, when working with large datasets, data security becomes a significant concern. Excel provides a feature to protect the data by locking the cells or rows in a worksheet, and this prevents users from altering the content accidentally.

Locking the rows in an Excel worksheet is a great way to ensure that important data is not accidentally overwritten. But how do you go about locking a row in Excel? In this article, we'll take a closer look at how to lock a row in Excel and provide some code examples to help you get started.

Why Lock Rows in Excel?

Locking rows in Excel is a great way to prevent users from accidentally changing critical data. For instance, if you have a sales worksheet containing information on the commissions earned by each salesperson, you would want to lock the rows with the commission data. This will prevent the accidental deletion or modification of crucial details that could affect your calculations.

Another primary reason for locking rows is password protection. If your Excel worksheet contains data you consider confidential, you wouldn't want unauthorized persons to access or modify the data. In this case, lock the rows to prevent unwanted access and changes.

How to Lock Rows in Excel

Locking rows in Excel is a straightforward process. The general steps include:

  • Select the rows or cells that you wish to lock
  • Go to the "Home" tab and select the "Format" dropdown menu
  • Click on the "Lock Cell" option, and the selected cells or rows will now be locked

Alternatively, you can use VBA code to lock rows in Excel. Here's how:

  1. Open a new Excel workbook.

  2. Press Alt + F11 to open the Visual Basic Editor.

  3. In the Editor window, select the VBA project that you wish to modify.

  4. Click on "Insert" in the menu and select "Module."

  5. In the Module window, type the following code:

Private Sub LockRows()
Rows("3:3").Locked = True
Rows("5:5").Locked = True
End Sub

This code allows you to lock any number of rows by changing the row numbers in the code.

  1. Once you've entered the appropriate code, close the Visual Basic Editor and return to the Excel worksheet.

  2. Now, select the rows or cells you wish to lock.

  3. Go to "Tools" > "Protection" > "Protect Sheet."

  4. Check the "Protect worksheet and contents of locked cells" checkbox.

  5. Enter a password if you wish to password-protect the worksheet.

  6. Click "OK" and save the worksheet.

Now that you've locked your rows, anyone who tries to modify the content of the protected rows will be prompted to enter a password. The password can be shared with authorized users to enable them to access the contents of the protected rows.


In conclusion, Excel allows you to protect data by locking rows or cells in a worksheet. Locking rows prevents users from accidentally modifying crucial data and adds an extra layer of security for confidential data. We've outlined two methods for locking rows in Excel; the one you choose will depend on your level of comfort with Excel and Visual Basics. You can use the general steps provided or go step-by-step using the VBA code example provided. With a little practice, locking rows in Excel will become second nature to you, and you'll be able to work with your Excel data more confidently.

Sure! Let's dive deeper into some of the topics covered in the article.

Lock Cells vs. Lock Worksheet

When you protect an Excel worksheet, you have two options: you can lock the worksheet, or you can lock individual cells or ranges of cells. Locking the worksheet prevents users from making any changes to the sheet, while locking specific cells or ranges allows you to restrict access to certain parts of the worksheet.

For instance, if you have some cells containing formulas, you can lock them to prevent accidental modification, while leaving other cells unlocked to allow data entry.

To lock specific cells or ranges, you need to select the cells or ranges you want to lock, right-click and choose Format Cells. In the Format Cells dialog box, go to the Protection tab and check the Locked checkbox. Next, go to the "Review" tab and click on the "Protect Sheet" button. You'll then be prompted to provide a password you'll use to protect the sheet, and to choose what types of changes users can make to the sheet.

Using VBA to Lock Rows

You can use VBA (Visual Basic for Applications) to automate routine tasks in Excel and to develop custom Excel applications. VBA is a powerful programming language that allows you to create macros and add-ins to extend the functionality of Excel.

To lock rows using VBA, you need to open the Visual Basic Editor (VBE) by pressing Alt + F11. In the editor, create a new module, and copy and paste the code from the article into the module. Next, go back to Excel, select the rows you want to lock, run the macro, and protect the sheet.

The VBA code example provided in the article locks rows 3 and 5 in the active worksheet, but you can modify the code to lock any number of rows. You can also use VBA to unlock rows or cells, protect or unprotect the worksheet, and perform other tasks.

Protecting Your Excel Workbook

In addition to protecting individual worksheets in Excel, you can also protect your entire workbook, including its structure and windows. This means that users cannot add, delete, move, or rename sheets, or access the VBE or other workbook elements, unless they have the password.

To protect your workbook, go to the Review tab, click on "Protect Workbook," and choose "Encrypt with Password." Enter your password and click OK. To make changes to the workbook, you'll need to unprotect it by entering the password.

While it's important to protect your Excel data, it's also important to choose secure passwords and to keep them safe. Never share your passwords with anyone who doesn't need them, and avoid using simple or predictable passwords. Additionally, make sure to back up your Excel data regularly, just in case of accidents or emergencies.

In conclusion, Excel provides several options for protecting your data, including locking cells and rows, protecting sheets and workbooks, and using VBA to automate tasks. These features can help you safeguard your Excel data and improve its accuracy and reliability.

Popular questions

  1. Why might you want to lock a row in Excel?
    Answer: You might want to lock a row in Excel to prevent accidental changes to important or confidential data.

  2. What are the two methods for locking rows in Excel?
    Answer: The two methods for locking rows in Excel are using the general steps provided or going step-by-step using VBA code.

  3. How do you lock specific cells in Excel, and what purpose do they serve?
    Answer: To lock specific cells in Excel, you need to select the cells or ranges you want to lock, go to the Format Cells dialog box, and check the Locked checkbox in the Protection tab. Locking specific cells in Excel is useful for protecting formulas or restricting access to certain parts of the worksheet.

  4. What is Visual Basic for Applications (VBA), and how can it be used to lock rows in Excel?
    Answer: Visual Basic for Applications (VBA) is a powerful programming language that allows you to create macros and add-ins to extend the functionality of Excel. You can use VBA to automate routine tasks in Excel, including locking and unlocking rows.

  5. How can you protect an entire Excel workbook, and what precautions should you take when creating a password?
    Answer: You can protect an entire Excel workbook, including its structure and windows, by going to the Review tab, clicking on "Protect Workbook," and choosing "Encrypt with Password." When creating a password, you should choose a secure one and avoid sharing it with anyone who doesn't need to know it. Additionally, it's important to back up your Excel data regularly to prevent the loss of important data.



As a developer, I have experience in full-stack web application development, and I'm passionate about utilizing innovative design strategies and cutting-edge technologies to develop distributed web applications and services. My areas of interest extend to IoT, Blockchain, Cloud, and Virtualization technologies, and I have a proficiency in building efficient Cloud Native Big Data applications. Throughout my academic projects and industry experiences, I have worked with various programming languages such as Go, Python, Ruby, and Elixir/Erlang. My diverse skillset allows me to approach problems from different angles and implement effective solutions. Above all, I value the opportunity to learn and grow in a dynamic environment. I believe that the eagerness to learn is crucial in developing oneself, and I strive to work with the best in order to bring out the best in myself.
Posts created 3245

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top