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 protect cells after data entry or input in Excel?

Supposing you have a worksheet and just a certain range of blank cells needs data entry, and after finishing inputting data, you need the cells to be locked automatically in order to prevent changes again. How can you do to achieve it? This article can help you.

Lock or protect cells after data entry or input with VBA code


Lock or protect cells after data entry or input with VBA code

For example, the certain range of blank cells is A1:F8. Please do as follows to lock these cells after data entry in Excel.

1. Please unlock this range first, select the cells and right-clicking, then choose Format Cells in the right-clicking menu, and in the Format Cells dialog box, unchecking the Locked box under the protection tab, and finally clicking the OK button. See screenshot:

2. Click Review > Protect Sheet. And specify a password to protect this worksheet.

3. Right click the sheet tab, select View Code from the right-clicking menu. Then copy and paste the below VBA code into the Code window. See screenshot:

VBA code: Lock or protect cells after data entry or input

Dim mRg As Range
Dim mStr As String

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Range("A1:F8"), Target) Is Nothing Then
    Set mRg = Target.Item(1)
    mStr = mRg.Value
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xRg As Range
    On Error Resume Next
    Set xRg = Intersect(Range("A1:F8"), Target)
    If xRg Is Nothing Then Exit Sub
    Target.Worksheet.Unprotect Password:="123"
    If xRg.Value <> mStr Then xRg.Locked = True
    Target.Worksheet.Protect Password:="123" 
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("A1:F8"), Target) Is Nothing Then
    Set mRg = Target.Item(1)
     mStr = mRg.Value
End If
End Sub

Note: In the code, “A1:F8” is the range you need to input data; and “123” is the password of this protected worksheet. Please change them as you need.

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

After finishing entering data to the cells of range A1:F8, they will be locked automatically. And you will get a prompt dialog box if you try to change any cell content of this range. See screenshot:


Office Tab - Tabbed Browsing, Editing, and Managing of Workbooks in Excel:

Office Tab brings the tabbed interface as seen in web browsers such as Google Chrome, Internet Explorer new versions and Firefox to Microsoft Excel. It will be a time-saving tool and irreplaceble in your work. See below demo:

Click for free trial of Office Tab!

Office Tab for Excel


Related articles:


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, 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 82% 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.
    M · 5 months ago
    it doesn't seem to work if i have some cells merged. is there a solution for that?
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi,
      If there are merged cells in the specified range, please try the following code.

      Dim mRg As Range
      Dim mStr As String

      Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      If Not Intersect(Range("A1:F8"), Target) Is Nothing Then
      Set mRg = Target.Item(1)
      mStr = mRg.Value
      End If
      End Sub
      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRg As Range
      On Error Resume Next
      Set xRg = Intersect(Range("A1:F8"), Target)
      If xRg Is Nothing Then Exit Sub
      Target.Worksheet.Unprotect Password:="123"
      If xRg.Value <> mStr Then xRg.MergeArea.Locked = True
      Target.Worksheet.Protect Password:="123"
      End Sub
      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Not Intersect(Range("A1:F8"), Target) Is Nothing Then
      Set mRg = Target.Item(1)
      mStr = mRg.Value
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    SURENDRA · 5 months ago
    SIR I SUCCESS TO PROTECT AND LOCK CELL BUT I WANT TO EDIT CELLS WHEN THEY LOCKED ONLY THROUGH CELLS CLICK AND ASK PASSWORD TO EDIT CELL . HOW TO DO THIS ..?
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi SURENDAR,
      Sorry can't help with that. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Prandeep · 6 months ago
    Sir, when pasting code after double click in selected entry cell which cell enter the data the cell did not permission to entry value please fix the problem modify the code.
    • To post as a guest, your comment is unpublished.
      crystal · 6 months ago
      Hi,
      The code has been updated with the problem solving, please have a try. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Jess · 7 months ago
    Hello, I'm trying to code so that a user can double-click and it will capture their username and timestamp. I was the cells to lock immediately after this is completed. I have the following in VBA, but it keeps debugging back to the "Target = Environ("USERNAME") & " " & Now()". I am VERY new and VERY inexperienced w/ VBA, so please bear with me if it's something small I'm doing wrong.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Target = Environ("USERNAME") & " " & Now()
    Cancel = True

    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xRg As Range
    On Error Resume Next
    Set xRg = Intersect(Range("A1:D45"), Target)
    If xRg Is Nothing Then Exit Sub
    Target.Worksheet.Unprotect Password:="GENERAL"
    xRg.Locked = True
    Target.Worksheet.Protect Password:="GENERAL"
    End Sub
    • To post as a guest, your comment is unpublished.
      crystal · 6 months ago
      Hi Jess,
      The below VBA code can help you.
      Note: When protecting the worksheet, please uncheck the "Select locked cells" option in the Protect sheet dialog box. Thank you for your comment.

      Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      Dim xRg As Range
      On Error Resume Next
      If ProtectContents Then
      Set xRg = Intersect(Range("A1:D4"), Target)
      If xRg Is Nothing Then Exit Sub
      Target.Worksheet.Unprotect Password:="123"
      Target = Environ("USERNAME") & " " & Now()
      Target.Locked = True
      Target.Worksheet.Protect Password:="123"
      Cancel = True
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Nazmul · 7 months ago
    when I get out of the currently working file and opened it again I found that new cell not locked after data entry, only the previous lock cell found lock. any solution
    • To post as a guest, your comment is unpublished.
      crystal · 6 months ago
      Hi Nazmul,
      You need to save the workbook as an Excel Macro-Enabled Workbook before closing it.