Note: The other languages of the website are Google-translated. Back to English

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:


Related articles:


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (74)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Hello, I have a spreadsheet with the range A3:AN219, i would like to protect this range as and when input complete. Please help me on this. I tried above code but it is not working for me
This comment was minimized by the moderator on the site
This is not working when it reopen excel file please help
This comment was minimized by the moderator on the site
Try this code instead: Private Sub Worksheet_Change(ByVal Target As Range) Dim MyRange As Range Set MyRange = Intersect(Range("A1:D100"), Target) If Not MyRange Is Nothing Then Sheets("Sheet1").Unprotect password:="hello" MyRange.Locked = True Sheets("Sheet1").Protect password:="hello" End If End Sub And remember to change range (A1:D100), password (hello) and sheet (Sheet1) names/numbers if it does not match the above :)
This comment was minimized by the moderator on the site
Thanks for the code. Please let me know how to do auto lock the cells (similar to the ones listed by you) ONLY AFTER saving the file
This comment was minimized by the moderator on the site
Hello Sir , I am facing one problem with the same code while using this code with other cobe in VBA. Please suggest me some solution Thanks and Regards Gourav
This comment was minimized by the moderator on the site
Dear Gourav,
Sorry can't solve this problem.
This comment was minimized by the moderator on the site
I tried the code and seemed to work somewhat. It does however allow me to delete the date entered into the cell but only stops me when I try to enter something else. Is there a way to keep data from being deleted?
This comment was minimized by the moderator on the site
Dear Stacy,
The same problem does not appear in my case. The code prevent users from entering as well as deleting data from the specified range. Would you provide your Office version for further testing?
This comment was minimized by the moderator on the site
For anyone who might have stumbled here since this comment. If you are having to unlock the worksheet EVERYTIME you fill in a cell then you need to first, - Unlock the spreadsheet - Highlight the area of the worksheet the code applies over - Right-click and go to 'format cells' - Go to the far right tab called 'Protection' and UNCHECK the 'Locked' section (Even if It appears as a solid fill instead of a tick) After that you will be able to enter data in multiple cells without having to unlock everytime. Bear in mind if you need to revisit locked cells to delete or edit information you may need to repeat the steps above. Hope this helps.
This comment was minimized by the moderator on the site
i like to lock only the edited cells. once i enter any data to the empty cells, i require to lock the edited cell to be automitically locked but not the empty cells.
This comment was minimized by the moderator on the site
Dear Subhash,
After using the code, only the edited cells are locked in the specified range. And you can still enter data into the empty cells in the specified range as you need. After fill in the blank cell, it will be locked automatically.
This comment was minimized by the moderator on the site
No, it locks the whole range at once after a data input into a cell in the range
This comment was minimized by the moderator on the site
Dear pradip,
Which Office version do you use?
This comment was minimized by the moderator on the site
Thanx for the coding, but I've a problem to put 2 coding in 1 sheet, pls help.
This comment was minimized by the moderator on the site
Dear Fair,
Sorry can't solve this problem.
This comment was minimized by the moderator on the site
Hi,
Sir i want to lock after one time input data !
Like i am following one customer and write status , then any more employee cant edit or modify that !
This comment was minimized by the moderator on the site
Good Day,
Method in this post can also help you to solve this problem.
This comment was minimized by the moderator on the site
cells are not even editable...what went wrong don't understand pls help
This comment was minimized by the moderator on the site
Good Day,
At first, you need to set specified cells to Unlocked for editig, and then protect the worksheet. And finally apply the VBA script.
This comment was minimized by the moderator on the site
is it have to coding?
This comment was minimized by the moderator on the site
Hi


i want it to lock the range after entry into one cell in the range please can you advise how i need to edit this to do this thank you
This comment was minimized by the moderator on the site
Hi
editing the code is there a way i can get it to lock all the cells in the range after data has been entered into one cell within that range ? so they can only enter data into one cell within the range not multiple.


thank you
This comment was minimized by the moderator on the site
Dear Jackie,
The below VBA code can help you solve the problem.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
Dim xSRg As Range
On Error Resume Next
Set xSRg = Range("A1:F8")
Set xRg = Intersect(xSRg, Target)
If xRg Is Nothing Then Exit Sub
Target.Worksheet.Unprotect Password:="123"
xSRg.Locked = True
Target.Worksheet.Protect Password:="123"
End Sub
This comment was minimized by the moderator on the site
Hello,
Is there a way to lock only cells rather than the whole sheet? For example, if a user enters "Yes" in A2, then A2 would lock immediately as to not allow any changes. I would still like if others could still edit any other cell too. Thanks!
This comment was minimized by the moderator on the site
Hi Smith,
The below VBA code can help you solving the problem. Please have a try and thank you for your comment.

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Target.Worksheet.Unprotect Password:="123"
Target.Locked = True
Target.Worksheet.Protect Password:="123"
End Sub
This comment was minimized by the moderator on the site
Good Morning,

Is there anyway to prevent a user from right clicking "view code" and seeing the admins password?
This comment was minimized by the moderator on the site
Never mind, I got it.
This comment was minimized by the moderator on the site
How? i didn't get it...
This comment was minimized by the moderator on the site
hello there need some help


im actually doing a working roster for a large group of people, and this sheet is accessible by all so that they can key in their requests for days off/ annual leaves etc. however i want to only limit a number of people on leave for each day (maximum 5 on leave) and after 5 leave requests are keyed in for the day, no body else can fill anymore requests for that particular date.


is there any code/function that will calculate the number of specific requests perday then when the quota is reached, then the other cells are blocked for the requests as to not exceed? thanks in advance
This comment was minimized by the moderator on the site
Good day,
Welcome to post any question in our forum: https://www.extendoffice.com/forum.html.
You will get more Excel supports from our professional or other Excel fans.
This comment was minimized by the moderator on the site
Hi - this post was very helpful and works perfectly. However, my filters stop working when the cells lock. Is there a way around this? Thanks!
This comment was minimized by the moderator on the site
Hi Kim,
Sorry can't help with this. The filter feature is disabled in a protected worksheet by default.
This comment was minimized by the moderator on the site
Thanks man, This Saved my job : D
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Hi Nazmul,
You need to save the workbook as an Excel Macro-Enabled Workbook before closing it.
There are no comments posted here yet
Load More
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations