Select unlocked cells in Excel VBA

There are two different cases you might deal with,

  • Lock only few specific cells and rest of the cells in the work sheet are not locked(means you can edit them)
  • Lock the Enitre worksheet except few cells

Lock only few specific cells and rest of the cells in the work sheet are not locked

Steps:

  • Lock the selected cells
  • Provide the password

Note: If you wont provide the password then your cells will not be locked.

Code :

Sub sumit() 

  Dim mainworkBook As Workbook

  Set mainworkBook = ActiveWorkbook

  mainworkBook.Sheets("Main").Range("A1:C5").Value = "Locked"

   mainworkBook.Sheets("Main").Range("A1:C5").Locked = True

   ActiveSheet.Protect Password:="xx"
 

End Sub

Select unlocked cells in Excel VBA
Cell Lock

Try editing any cell which is locked.

Select unlocked cells in Excel VBA
Cell Lock 2

Lock the Enitre worksheet except few cells

  • Firsr choose the cells which you dont want to lock
  • Then lock the entire worksheet and provide the password.

Code:

Sub sumit()

  Dim mainworkBook As Workbook

  Set mainworkBook = ActiveWorkbook

  mainworkBook.Sheets("Main").Range("A1:C5").Value = "Free"

  mainworkBook.Sheets("Main").Range("A1:C5").Locked = False

   mainworkBook.Sheets("Main").Protect passowrd = "xx"

End Sub

Select unlocked cells in Excel VBA
unlock specific cells -1

Change the cells which are free, and try changing rest of the cells

Select unlocked cells in Excel VBA
unlock specific cells -2

  • Password Reset

This Excel VBA Protect Sheet Allow Select Locked Cells Tutorial is accompanied by an Excel workbook with the data and VBA code I use when describing the step-by-step process below. Get this example workbook (for free) by clicking the button below.


Select unlocked cells in Excel VBA
Select unlocked cells in Excel VBA

The VBA code in the Excel workbook that accompanies this Excel VBA Protect Sheet Allow Select Locked Cells Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section below.

Table of Contents

  • Related Excel Macro and VBA Training Materials and Resources
  • The VBA Protect Sheet Allow Select Locked Cells Snippet Template/Structure
  • The Example Before VBA Protect Sheet Allow Select Locked Cells
  • Step 1: Refer to Sheet
    • Step 1 Example
  • Step 2: Lock Cells
    • Step 2 Example
  • Step 3: Protect the Sheet
    • Step 3 Example
  • Step 4: Allow the User to Select Locked Cells
    • Step 4 Example
  • Download the VBA Protect Sheet Allow Select Locked Cells Example Workbook
  • Related Excel Macro and VBA Training Materials and Resources

The following Excel Macro and VBA Tutorials may help you better understand and implement the contents below.

  • Tutorials about general macro and VBA constructs and structures:
    • Tutorials for Beginners:
      • Excel Macros: Click here to open.
      • Excel VBA: Click here to open.
    • Enable macros in Excel: Click here to open.
    • Work with the Visual Basic Editor (VBE): Click here to open.
    • Create Sub procedures: Click here to open.
    • Refer to objects (click here to open), including:
      • Sheets: Click here to open.
      • Cell ranges: Click here to open.
    • Work with:
      • Properties: Click here to open.
      • Methods: Click here to open.
  • Tutorials with practical VBA applications and macro examples:
    • Activate workbook: Click here to open.
    • Create new workbook: Click here to open.
    • Open workbook: Click here to open.
    • Delete sheet: Click here to open.

This Excel VBA Protect Sheet Allow Select Locked Cells Tutorial is part of a more comprehensive series of Excel VBA Protect or Unprotect Sheet Tutorials.

  • Excel VBA Protect Sheet Without Password in 2 Easy Steps: Click here to open.
  • Excel VBA Unprotect Sheet Without Password in 2 Easy Steps: Click here to open.
  • Excel VBA Unprotect Sheet with Password in 2 Easy Steps: Click here to open.
  • Excel VBA Protect Sheet with Password in 2 Easy Steps: Click here to open.
  • Excel VBA Protect Sheet Allow Filter in 2 Easy Steps: Click here to open.

You can find more Excel and VBA Tutorials in the organized Tutorials Archive: Click here to visit the Archives.

If you want to learn how to automate Excel (and save time) by working with macros and VBA, you may be interested in the following Premium Excel Macro and VBA Training Materials:

  • Premium Courses at the Power Spreadsheets Academy: Click here to open.
  • Books at the Power Spreadsheets Library: Click here to open.
  • VBA Cheat Sheets: Click here to open.

If you want to save time when working with macros and VBA, you may be interested in AutoMacro: Click here to learn more about AutoMacro (affiliate link). AutoMacro is an add-in for VBA that installs directly into the VBE. Depending on the version, AutoMacro comes loaded with:

  • Code generators.
  • An extensive code library.
  • The ability to create your own code library.
  • Advanced coding tools.

If you need consulting services, you may want to consider working with ExcelRescue. ExcelRescue is my usual suggestion for people who (like you) may need help with Excel tasks/projects: Click here to visit ExcelRescue (affiliate link).

How do I select only unlocked cells in Excel?

You can also press Ctrl+Shift+F or Ctrl+1. In the Format Cells popup, in the Protection tab, uncheck the Locked box and then click OK.

How do you unlock cells in Excel VBA?

You aren't able to unlock the cells, even using VBA code, if the sheet is protected. So if you want to use code to unlock some cells, you have to unprotect the workbook/worksheet first.

How do I select individual cells in Excel VBA?

Selecting a Single Cell Using VBA Range(“A1”) tells VBA the address of the cell that we want to refer to. Select is a method of the Range object and selects the cells/range specified in the Range object. The cell references need to be enclosed in double quotes.