Get our FREE VBA eBook of the 30 most useful Excel VBA macros.

Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Claim your free eBook

How to select all unlocked cells in Excel

Select locked cells

Spreadsheets are easy to break.  All it takes is one press of a key and suddenly every formula is displaying in an error message.  That’s not good!  This is why we apply worksheet protection, to prevent user error.

Some think that worksheet protection is secure, however password protected worksheets can easily be unlocked with a macro.  Therefore, any user who really wants to break our spreadsheet can.  The real benefit of protection is therefore, not to lock down the worksheet, but to prevent basic user error.

Allow users to change cells when worksheet is protected

We can protect an entire worksheet.  But we can also set specific cells to be unlocked, this enables the user to interact only with the parts of the worksheet that we want them to.

Whilst the worksheet is unlocked, click on any Cell and open the Format Cells window.  On the Protection tab there is the option to lock or unlock the Cell.

Format Cells - lock and unlock

To apply this with VBA we could use the following code:

Sub UnlockSelectedCells()

Selection.Locked = False

End Sub

Or to reverse it back to locked

Sub LockSelectedCells()

Selection.Locked = True

End Sub

How to select all unlocked cells

But here is the challenge, without going through every cell, how do we know which are locked or unlocked?  What if we want to apply formatting to only the unlocked cells how could we do that easily?

The Go To Special… menu has a lot of good selection options, but selecting unlocked cells is not one of them.

It’s possible to tab through unlocked cells when the worksheet is protected, but that can be time consuming and not particularly convenient.

It’s much faster to apply some VBA to select only the unlocked cells.

The following code will select all the unlocked cells:

  • from the used range of the worksheet if only one cell is selected
  • from the selected range  if more than one cell is selected.
Sub SelectAllUnlockedCells()

Dim c As Range
Dim unlockedCells As Range
Dim fullRange As Range
Dim rangeDescription As String

'Count cells in selection .If 1 cell then search used range
'otherwise search the selected range
If Selection.Cells.Count > 1 Then
    Set fullRange = Selection
    rangeDescription = "selected cells"
    Set fullRange = ActiveSheet.UsedRange
    rangeDescription = "active range"
End If

'Loop through each cell in the chosen range of active sheet
For Each c In fullRange

    If c.Locked = False Then

        'If find first cell set the variable, otherwise add
        'to existing list of cells
        If unlockedCells Is Nothing Then
            Set unlockedCells = c
            Set unlockedCells = Union(unlockedCells, c)
        End If

    End If


'Select the range of unlocked cells
If Not unlockedCells Is Nothing Then

    MsgBox "There are no unlocked cells in the " _
        & rangeDescription & ": " & fullRange.Address
End If

End Sub

And that’s it.  Simple right.

The code will also work with protected worksheets, provided the protection settings applied allow the selection of unlocked cells.

All Select Unlocked Cells


If you regularly build Excel models and applications for other to use, this macro can save you time and reduce errors.  Add this code to a standard module inside your Personal Macrobook to ensure it’s always available when you need it.

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).

Do you need help adapting this post to your needs?

I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you're still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise.  List all the things you've tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don't go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Leave a Reply

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