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 or unlock cells based on values in another cell in Excel?

In some cases, you may need to lock or unlock cells based on values in another cell. For example, you need the range B1:B4 to be unlocked if cell A1 contains value “Accepting”; and to be locked if cell A1 contains value “Refusing”. How can you do to achieve it? This article can help you.

Lock or unlock cells based on values in another cell with VBA code

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 bubble Lock or unlock cells based on values in another cell with VBA code


The following VBA code can help you lock or unlock cells based on value in another cell in Excel.

1. Right click the sheet tab (the sheet with cells you need to lock or unlock based on values in another cell), and click View Code from the right-clicking menu.

2. Then copy and paste the following VBA code into the Code window.

VBA code: Lock or unlock cells based on values in another cell

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A1") = "Accepting" Then
        Range("B1:B4").Locked = False
    ElseIf Range("A1") = "Refusing" Then
        Range("B1:B4").Locked = True
    End If
End Sub

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

From now on, when you enter value “Accepting” into cell A1, the range B1:B4 is unlocked.

When entering value “Refusing” into cell A1, the specified range B1:B4 is locked automatically.


arrow blue right bubbleRelated articles:


Recommended 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.
    Mitchyll · 6 months ago
    How would the code be if I wanted to lock a block of cells (Rows 6, 7, and 8/Letters D through U as well as cells F5 and J5) and have them unlock when I put an "X" in cell E5? Thanks in advance!
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi MitchyII,
      Do you mean the specified block of cells have been locked manually in advance and just want to unlock them by typing an "X" in cell E5?
      If remove "X" from cell E5, you do want to lock the ranges again?
      I need more details of the problem.
      Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Joe · 1 years ago
    Would you kindly advice me on how to correct this? Thank you in advance.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A:A") = "SLOW MOVING" Then
    Range("B:B").Locked = True
    ElseIf Range("A:A") = "OVER STOCK" Then
    Range("B:B").Locked = True
    ElseIf Range("A:A") = "NORMAL" Then
    Range("B:B").Locked = False
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      Noexpert · 1 years ago
      Not being a VB expert i would say you have too many "Elseif" - if you change them all to just IF except for the last one then hopefully that will work.
      Basically If X do this, If Y do this, if Z do this, if none of those - do this.
  • To post as a guest, your comment is unpublished.
    Joe · 1 years ago
    Can you guide me on what's wrong here please? Thank you in advance.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("K:K") = "OVER STOCK" Then
    Range("S:S").Locked = True
    ElseIf Range("K:K") = "SLOW MOVING" Then
    Range("S:S").Locked = True
    ElseIf Range("K:K") = "NORMAL" Then
    Range("S:S").Locked = False
    ElseIf Range("K:K") = "SHORTAGE" Then
    Range("S:S").Locked = False
    End If
    End Sub
  • To post as a guest, your comment is unpublished.
    KB · 1 years ago
    I prepare a warehouse stock management in excel template.To deliver a stock i have to issue a gate pass.I want to each gate pass,corresponding data will be updated in daily stock take page.With the change of gate pass serial no,the row will be locked and next will be filled up.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good Day,
      Would be nice if you can upload your workbook here. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Andor Veres · 1 years ago
    Hi,
    I am really new to this.
    I have been trying to put an invoicing system together in excel.
    I created 3 sheets.
    1. Invoice template (Invoice)- Just a generic invoice that is sent to my agents weekly.

    2. A data sheet (Data sheet) to be exact - where the invoice can read the company name adress etc, so if anything changes the invoice will be automatically updated.

    3. A calendar tab (Calendar 2018) to be exact - that is referenced in the invoice template, and puts the corresponding date and invoice number on the actual invoices.

    What I want to do.
    The calendar tab would be my main page, I added a status drop down cell for each week with options "Active" and "Closed". I would like to Lock the whole "Invoice" tab if the corresponding cell is set to "Closed".

    I hope you guys understand what I am trying to do.
    Thanks in advance.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Ando Veres.
      The below VBA code can help you. Please place the code into the sheet code window of Calendar 2018 change A1 to your drop down cell. Thank you.

      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRg As Range
      On Error Resume Next
      Set xRg = Intersect(Target, Range("A1"))
      If xRg Is Nothing Then Exit Sub
      If Target.Validation.Type >= 0 Then
      If Target.Value = "Closed" Then
      Sheets("Data Sheet").Protect
      ElseIf xRg.Value = "Active" Then
      Sheets("Data Sheet").Unprotect
      End If
      End If
      End Sub