Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to lock specified cells without protecting the whole worksheet in Excel?

Normally, you need to protect the whole worksheet for locking cells from editing. Is there any method to lock cells without protecting the whole worksheet? This article recommend a VBA method for you.

Lock specified cells without protecting the whole worksheet with VBA

Excel Productivity Tools

Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial

Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately. 60-day Unlimited Free Trial


arrow blue right bubbleLock specified cells without protecting the whole worksheet with VBA


Supposing you need to lock cell A3 and A5 in current worksheet, the following VBA code will help you achieve it without protecting the whole worksheet.

1. Right click the sheet tab, and select View Code from the right-clicking menu.

2. Then copy and paste the below VBA code into the Code window. See screenshot:

VBA code: Lock specified cells without protecting the whole worksheet

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 Then
        If Target.Row = 3 Or Target.Row = 5 Then
            Beep
            Cells(Target.Row, Target.Column).Offset(0, 1).Select
        End If
    End If
End Sub

Note: In the code, Column 1, Row = 3 and Row = 5 indicate cell A3 and A5 in current worksheet will be locked after running the code. You can change them as you need.

3. Press the Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.

Now cell A3 and A5 are locked in current worksheet. If you try to select cell A3 or A5 in current worksheet, the cursor will be moved to the right adjacent cell automatically.


arrow blue right bubbleRelated articles:


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  • Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  • Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  • Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  • Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  • No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  • Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  • 60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  • Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Charlie · 3 months ago
    Hello,

    I used the code provided to Carlos and it did exactly what I wanted it to. Is there a way to have some ranges offset within the ROW to the right of the protected range (as the "Carlos" code already does), but have other ranges offset within the COLUMN to the cell directly below the protected range? I tried entering the "Carlos" code twice and changing the offset, but I received a variety of errors.

    Thank you
    • To post as a guest, your comment is unpublished.
      crystal · 3 months ago
      Hi Charlie,
      Sorry can't help you with that yet. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Espen · 4 months ago
    Is there any function to set for example Row 2 cell 13 to 900? Or do i need to manually punch each cell name in the code?
  • To post as a guest, your comment is unpublished.
    Christophe Bourquard · 6 months ago
    Bonjour,
    Comment faire pour verrouiller de la cellule B8 à B10000?
    D'avance merci de votre réponse.
    Christophe
  • To post as a guest, your comment is unpublished.
    Erin · 10 months ago
    Dear Crystal,

    Thank you so much for this! It works perfectly.

    I have used the code in Carlos' comment to run the macro automatically as you open the file. I was wondering if there is a way of having an "Undo" button or something like it, that allows you to cancel that code and therefore, unlock those ranges that have been locked.

    I want those to be locked most of the time, but I would like to unlock them should I need to change any of the data. The only reason I am not protecting the whole sheet is because, if I do, it deactivates the ability to expand on tables. That in turn, expand on a line chart.

    Thanks so much for the help!
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi Erin,
      You can manually break the code by clicking the Break button in the Microsoft Visual Basic for Applications window to unlock those ranges. And run the code to activate it again. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Carlos · 1 years ago
    Dear Crystal,

    Is there anyway that this macro can be run automatically upon opening the file, or upon clicking any cell. I don't want people to have to run the macro manually in order for the required ranges to be protected.

    Also, I will need to protect 12 non adjacent ranges. For example: I11:I20 and K11:K20 and M11:20 etc... How do I do this?

    Last, and this one might be asking too much, but is it possible to apply the protection to the example ranges that I provided above, BUT then extend the protection down additional rows as new ones are added? In other words, protection would be applied to I11:I20 and K11:K20 and M11:20, but a user would be able to add a new row (row 21), with new data, but once the new row is added, then the protection would apply to I11:I21 and K11:K21 and M11:21. Am I asking for the moon? :-)

    Thank you for everything that you've already provided! I can't thank you and other people like you enough for sharing the your knowledge. Amazing!
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Carlos,
      The following VBA code can help you solving the problem. Please fill in your ranges in the fourth line of the code and press the Alt + Q keys to return to the worksheet. Then please shift to other worksheet and then go back to current sheet to activate the code. Thank you for your comment.

      Dim xRg As Range
      Private Sub Worksheet_Activate()
      If xRg Is Nothing Then
      Set xRg = Union(Range("I10:I20"), Range("K10:K20"), Range("M10:M20"), Range("O10:O20"))
      End If
      End Sub
      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim I As Integer
      Dim xRgNew As Range
      Dim xRgLCell As Range
      On Error Resume Next
      Application.EnableEvents = False
      For I = 1 To xRg.Areas.Count
      Set xRgLCell = xRg.Areas.Item(I)
      Set xRgLCell = xRgLCell(xRgLCell.Count).Offset(1, 0)
      If Target.Address = xRgLCell.Address Then
      If xRgNew Is Nothing Then
      Set xRgNew = Target
      Else
      Set xRgNew = Union(xRgNew, Target)
      End If
      End If
      Next
      Set xRg = Union(xRg, xRgNew)
      Application.EnableEvents = True
      End Sub
      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      On Error GoTo Exitsub
      If (Not Intersect(xRg, Target) Is Nothing) And (Target.Count = 1) Then
      Target.Offset(0, 1).Select
      End If
      Exitsub:
      End Sub