How To Make Cell Not Editable In Excel Using Vba

How to Make a Cell Not Editable in Excel Using VBA

In Excel, you can use Visual Basic for Applications (VBA) to make cells not editable when a worksheet is protected. This can be useful if you want to prevent users from changing specific cells or data in your Excel workbook. Here’s how you can achieve this with VBA:

Step 1: Open the Visual Basic Editor

To open the Visual Basic Editor, press Alt + F11 on your keyboard while in Excel.

Step 2: Insert a New Module

In the Visual Basic Editor, click on Insert in the menu and then select Module to insert a new module.

Step 3: Write the VBA Code

In the new module, you can write the VBA code to make a specific cell not editable. Here’s an example code snippet:


    Sub MakeCellNotEditable()
        Worksheets("Sheet1").Range("A1").Locked = True
    End Sub
  

In this example, the code makes cell A1 in Sheet1 not editable by setting its Locked property to True.

Step 4: Protect the Worksheet

After writing the code, you need to protect the worksheet to enforce these cell protection settings. Here’s how you can do it:

  • In Excel, go to the Review tab.
  • Click on Protect Sheet.
  • Optionally, you can set a password to unprotect the sheet later if needed.
  • Click on OK.

Step 5: Test the Workbook

Save your workbook and test it by trying to edit the cell that you made not editable using the VBA code. You should find that the cell is now protected and cannot be modified.

This is a basic example to make a single cell not editable. However, you can modify the code to make multiple cells or ranges not editable by adjusting the range in the VBA code.

Related Post

Leave a comment