Note: The other languages of the website are Google-translated. Back to English
Σύνδεση  \/ 
x
or
x
Εγγραφή  \/ 
x

or

Πώς να κλειδώσετε ή να προστατεύσετε κελιά μετά την εισαγωγή δεδομένων ή την εισαγωγή στο Excel;

Ας υποθέσουμε ότι έχετε ένα φύλλο εργασίας και μόνο ένα συγκεκριμένο εύρος κενών κελιών χρειάζεται εισαγωγή δεδομένων και αφού ολοκληρώσετε την εισαγωγή δεδομένων, θα πρέπει να κλειδωθούν αυτόματα τα κελιά για να αποφευχθούν οι αλλαγές ξανά. Πώς μπορείτε να το πετύχετε; Αυτό το άρθρο μπορεί να σας βοηθήσει.

Κλείδωμα ή προστασία κελιών μετά την εισαγωγή δεδομένων ή εισαγωγή με κωδικό VBA


Κλείδωμα ή προστασία κελιών μετά την εισαγωγή δεδομένων ή εισαγωγή με κωδικό VBA

Για παράδειγμα, το συγκεκριμένο εύρος των κενών κελιών είναι A1: F8. Κάντε τα εξής για να κλειδώσετε αυτά τα κελιά μετά την εισαγωγή δεδομένων στο Excel.

1. Ξεκλειδώστε πρώτα αυτό το εύρος, επιλέξτε τα κελιά και κάντε δεξί κλικ και, στη συνέχεια, επιλέξτε κύτταρα μορφή στο μενού με δεξί κλικ και στο κύτταρα μορφή πλαίσιο διαλόγου, καταργώντας την επιλογή του Κλειδωμένο πλαίσιο κάτω από το προστασία καρτέλα και, τέλος, κάνοντας κλικ στο OK κουμπί. Δείτε το στιγμιότυπο οθόνης:

2. κλικ Βαθμολογία Κριτικής > Προστατέψτε το φύλλο. Και καθορίστε έναν κωδικό πρόσβασης για την προστασία αυτού του φύλλου εργασίας.

3. Κάντε δεξί κλικ στην καρτέλα φύλλων, επιλέξτε Προβολή κωδικού από το μενού με δεξί κλικ. Στη συνέχεια, αντιγράψτε και επικολλήστε τον παρακάτω κώδικα VBA στο παράθυρο Code. Δείτε το στιγμιότυπο οθόνης:

Κωδικός VBA: Κλείδωμα ή προστασία κελιών μετά την εισαγωγή ή εισαγωγή δεδομένων

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: Στον κώδικα, το "A1: F8" είναι το εύρος που χρειάζεστε για την εισαγωγή δεδομένων. και "123" είναι ο κωδικός πρόσβασης αυτού του προστατευμένου φύλλου εργασίας. Αλλάξτε τα όπως χρειάζεστε.

4. Τύπος άλλος + Q ταυτόχρονα για να κλείσετε το Microsoft Visual Basic για εφαρμογές παράθυρο.

Αφού ολοκληρώσετε την εισαγωγή δεδομένων στα κελιά της περιοχής A1: F8, θα κλειδωθούν αυτόματα. Και θα λάβετε ένα γρήγορο παράθυρο διαλόγου εάν προσπαθήσετε να αλλάξετε οποιοδήποτε περιεχόμενο κελιού αυτού του εύρους. Δείτε το στιγμιότυπο οθόνης:


Σχετικά Άρθρα:


Τα καλύτερα εργαλεία παραγωγικότητας του Office

Το Kutools για Excel λύνει τα περισσότερα από τα προβλήματά σας και αυξάνει την παραγωγικότητά σας κατά 80%

  • Επαναχρησιμοποίηση: Εισαγάγετε γρήγορα σύνθετοι τύποι, γραφήματα και οτιδήποτε έχετε χρησιμοποιήσει στο παρελθόν. Κρυπτογράφηση κυττάρων με κωδικό πρόσβασης Δημιουργία λίστας αλληλογραφίας και στείλτε email ...
  • Super Formula Bar (επεξεργαστείτε εύκολα πολλές γραμμές κειμένου και τύπου). Διάταξη ανάγνωσης (εύκολη ανάγνωση και επεξεργασία μεγάλου αριθμού κελιών). Επικόλληση σε φιλτραρισμένο εύρος...
  • Συγχώνευση κελιών / σειρών / στηλών χωρίς απώλεια δεδομένων · Περιεχόμενο διαχωρισμού κελιών Συνδυάστε διπλές σειρές / στήλες... Αποτροπή διπλών κυττάρων; Συγκρίνετε τα εύρη...
  • Επιλέξτε Διπλότυπο ή Μοναδικό Σειρές; Επιλέξτε Κενές σειρές (όλα τα κελιά είναι κενά). Σούπερ εύρεση και ασαφής εύρεση σε πολλά βιβλία εργασίας. Τυχαία επιλογή ...
  • Ακριβές αντίγραφο Πολλαπλά κελιά χωρίς αλλαγή της αναφοράς τύπου. Αυτόματη δημιουργία αναφορών σε πολλαπλά φύλλα? Εισαγωγή κουκκίδων, Πλαίσια ελέγχου και άλλα ...
  • Εξαγωγή κειμένου, Προσθήκη κειμένου, Κατάργηση κατά θέση, Αφαιρέστε το διάστημα; Δημιουργία και εκτύπωση υποσύνολων σελιδοποίησης. Μετατροπή περιεχομένου και σχολίων μεταξύ κελιών...
  • Σούπερ φίλτρο (αποθηκεύστε και εφαρμόστε σχήματα φίλτρων σε άλλα φύλλα). Προηγμένη ταξινόμηση ανά μήνα / εβδομάδα / ημέρα, συχνότητα και άλλα. Ειδικό φίλτρο με έντονη, πλάγια ...
  • Συνδυάστε βιβλία εργασίας και φύλλα εργασίας; Συγχώνευση πινάκων βάσει βασικών στηλών. Διαχωρίστε τα δεδομένα σε πολλά φύλλα; Μαζική μετατροπή xls, xlsx και PDF...
  • Περισσότερα από 300 ισχυρά χαρακτηριστικά. Υποστηρίζει Office / Excel 2007-2019 και 365. Υποστηρίζει όλες τις γλώσσες. Εύκολη ανάπτυξη στην επιχείρηση ή τον οργανισμό σας. Πλήρεις δυνατότητες δωρεάν δοκιμής 30 ημερών. Εγγύηση επιστροφής χρημάτων 60 ημερών.
kte καρτέλα 201905

Το Office Tab φέρνει τη διεπαφή με καρτέλες στο Office και κάνει την εργασία σας πολύ πιο εύκολη

  • Ενεργοποίηση επεξεργασίας και ανάγνωσης καρτελών σε Word, Excel, PowerPoint, Publisher, Access, Visio και Project.
  • Ανοίξτε και δημιουργήστε πολλά έγγραφα σε νέες καρτέλες του ίδιου παραθύρου και όχι σε νέα παράθυρα.
  • Αυξάνει την παραγωγικότητά σας κατά 50% και μειώνει εκατοντάδες κλικ ποντικιού για εσάς κάθε μέρα!
κάτω μέρος γραφείου
Say something here...
symbols left.
You are guest
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.
    crystal · 3 months ago
    @daryll Hi,
    Which Excel version are you using?
  • To post as a guest, your comment is unpublished.
    daryll · 3 months ago
    Good day...

    Your tutorial is great!

    I ran across a Run-Time error '13': during selection change if I select entire row. What is the turn-around for this? Any insight is much appreciated.
  • To post as a guest, your comment is unpublished.
    crystal · 6 months ago
    @simon Hi simon,
    If you want to lock cells in D6:D36, H6:H35 and L6:L35 separately after finish entering data in each range. Please do as follows.
    1. Select these three ranges by holding the Ctrl key;
    2. Do as the post described in step 1 to unlock these three ranges;
    3. Protect your worksheet with a password (Here my password is 123. This password will be used in the below code);
    4. Right click the sheet tab and then paste the below VBA code into the Code editor, and then press Alt + Q keys to close the Microsoft Visual Basic for Applications window.
    Notes:
    1) In the code, you can change the ranges and password as you need;
    2)After pressing Alt + Q keys to close the code window, you need to shift to another worksheet and then go back to current sheet to make the code work. Otherwise, error will be occurred.
    Dim mRg As Range 'Updated by Extendoffice 20201030 Dim mStr As String Dim mStrAddress As String Dim mArr Private Sub Worksheet_Activate() On Error Resume Next Erase mArr() mStrAddress = "D6:D36,H6:H35,L6:L35" mArr = Split(mStrAddress, ",") End Sub Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim xI As Integer For xI = 0 To UBound(mArr) If Not Intersect(Range(mArr(xI)), Target) Is Nothing Then Set mRg = Target.Item(1) mStr = mRg.Value Exit For End If Next End Sub Private Sub Worksheet_Change(ByVal Target As Range) Dim xRg As Range Dim xI As Integer On Error Resume Next For xI = 0 To UBound(mArr) Set xRg = Null Set xRg = Intersect(Range(mArr(xI)), Target) If Not (xRg Is Nothing) Then Target.Worksheet.Unprotect Password:="123" If xRg.Value <> mStr Then xRg.Locked = True Target.Worksheet.Protect Password:="123" End If Next End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim xI As Integer On Error Resume Next For xI = 0 To UBound(mArr) If Not Intersect(Range(mArr(xI)), Target) Is Nothing Then Set mRg = Target.Item(1) mStr = mRg.Value End If Next End Sub
  • To post as a guest, your comment is unpublished.
    simon · 6 months ago
    Hi, This is all new to me. The formula is great. I want to lock cells D6:D36, H6:H35 & L6:L35 but can't get this to work. any help would be greatly appreciated.
  • To post as a guest, your comment is unpublished.
    kit · 6 months ago
    If Not Intersect(Range("CUSTOMER!"), Target) Is Nothing Then

    I got an error. I want to protect the whole sheet
  • To post as a guest, your comment is unpublished.
    scpsrm · 8 months ago
    Good afternoon ... thank you again for this great resource. I do have one question. We have a shared document that is used by multiple users for input purposes. We have noticed that if User A enters data in a given cell, User A cannot edit per the code above (which is exactly what we want) but User B who the document is also shared with can delete the data that User A entered. Is there a revision for the code above that could be included in a shared document that has multiple users that are entering data.
  • To post as a guest, your comment is unpublished.
    WGardner970 · 9 months ago
    @crystal Thank you so much for this, it is really helpful!

    One of my colleagues is working on a notebook that isn't able to install Microsoft Excel but through Microsoft Teams she can access this workbook.Something we were wondering about for another sheet would be if the password were typed into cell AZ1 then all the other cells would unlock, but if the password is not currently in the cell the cells would switch back to locked. Is this feasible?
  • To post as a guest, your comment is unpublished.
    Will · 9 months ago
    @crystal Thanks so much! I just got a chance to test this while setting up our logging sheet for next month and this works fantastic. One thing I did notice is that reading for "Complete" is case sensitive. Sometimes the logger will put in "complete" and the data limitation allows it. To include this, would I just copy that "If" to "End If" and put "complete"?
  • To post as a guest, your comment is unpublished.
    Yosi · 10 months ago
    Hi
    I want to auto lock cell while i'm saving my worksheet
    Can you help me how to do this in vba
  • To post as a guest, your comment is unpublished.
    crystal · 10 months ago
    @Will Hi Will,
    Try this VBA. When entering "Complete" in any cell in column E, the rest cells of that row will be locked.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    For Each cell In Range("E1:E500")
    If cell.Value = "Complete" Then
    ActiveSheet.Unprotect "Password"
    cell.EntireRow.Locked = True
    ActiveSheet.Protect "Password"
    End If
    Next cell

    End Sub
  • To post as a guest, your comment is unpublished.
    Will · 10 months ago
    Good morning,

    I want to make it so that when a specific cell="Complete" the rest of that row locks. How would I modify this? Let's say that someone enters data into columns A through D and then Column E they enter "Complete" to lock A:E for that row.

    Thank you,

    Will
  • To post as a guest, your comment is unpublished.
    crystal · 11 months ago
    @John Stephen Hi John,
    This can't be done in google sheet. Sorry for the inconvenience.
  • To post as a guest, your comment is unpublished.
    John Stephen · 1 years ago
    Hi,
    Can this be done in google sheet, if yes please let me know the procedure.

  • To post as a guest, your comment is unpublished.
    john · 1 years ago
    HI,

    Can this be done in Google sheet if yes how?
  • To post as a guest, your comment is unpublished.
    Skyfirst · 1 years ago
    For those who want to protect different range of cells you can edit the range in the code like what I did. Use "comma" if making multiple cells.
  • To post as a guest, your comment is unpublished.
    VALERY · 1 years ago
    hello if i have entered data from A1 right up to F and from A2 right up to F what will be the range that i have to input on the VBA code listed above in order to protect the cell A1,B1, C1, D1, E1, F1 AND A2, B2, C2, D2, E2, F2
  • To post as a guest, your comment is unpublished.
    jayaa · 1 years ago
    Hi,

    Please, can you help with my needs?

    I am looking for VBA code - Once I save the sheet then can't edit on used cells and only allow to enter in new cells. Save by clicking save symbol or close file.

    I tried various options but I unable to get them correctly, sometimes works and sometimes fail to work. Please help.
  • To post as a guest, your comment is unpublished.
    RUPESH · 1 years ago
    Hi
    how do i increase range? I have already done protect sheet and i want to increase rage how do i do it>

  • To post as a guest, your comment is unpublished.
    Daniel · 1 years ago
    @Jenny Unlock the Worksheet, and when protecting again, enable the edit object checkbox before confirming passwords
  • To post as a guest, your comment is unpublished.
    zxp · 1 years ago
    I am looking same sort of thing but data range is in different cells eg, B3:D44 and then F20:H122 and so on 5 or 6 different ranges, could you please elaborate how to make VB code for it?
  • To post as a guest, your comment is unpublished.
    Jenny · 1 years ago
    After implementing this code, I can no longer sign my signature in Excel's signature tool. What code can I add so that this won't occur? I can set it so that one can edit objects but as soon as I type something into one of my protected cells the options are reset and I can't use the signature box. Any help would be appreciated
  • To post as a guest, your comment is unpublished.
    crystal · 1 years ago
    @Ryan Bullock Good day,
    Can't help you solving this problem yet. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    crystal · 1 years ago
    @Jim Hi Jim,
    Didn't text in google sheet. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Jim · 1 years ago
    Hi, something like this in google sheet?
  • To post as a guest, your comment is unpublished.
    Ryan Bullock · 1 years ago
    Hello there, how can I use this so that the user inputs the data in a range and then must click a submit button/cell to lock the cells?
  • To post as a guest, your comment is unpublished.
    AW · 1 years ago
    If the cell is currently empty (but has previously been filled) is there a way to make it editable rather than protected?
  • To post as a guest, your comment is unpublished.
    crystal · 2 years ago
    @M 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.
    crystal · 2 years ago
    @SURENDRA Hi SURENDAR,
    Sorry can't help with that. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    M · 2 years 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.
    SURENDRA · 2 years 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 · 2 years ago
    @Prandeep 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.
    Prandeep · 2 years 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 · 2 years ago
    @Jess 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.
    crystal · 2 years ago
    @Nazmul Hi Nazmul,
    You need to save the workbook as an Excel Macro-Enabled Workbook before closing it.
  • To post as a guest, your comment is unpublished.
    Jess · 2 years 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.
    Nazmul · 2 years 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.
    shreedhar · 2 years ago
    Thanks man, This Saved my job : D
  • To post as a guest, your comment is unpublished.
    Max · 2 years ago
    @Stires62 How? i didn't get it...
  • To post as a guest, your comment is unpublished.
    crystal · 2 years ago
    @Kim Hi Kim,
    Sorry can't help with this. The filter feature is disabled in a protected worksheet by default.
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @max m. marcus 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.
  • To post as a guest, your comment is unpublished.
    Kim · 3 years ago
    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!
  • To post as a guest, your comment is unpublished.
    max m. marcus · 3 years ago
    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
  • To post as a guest, your comment is unpublished.
    Stires62 · 3 years ago
    @Stires62 Never mind, I got it.
  • To post as a guest, your comment is unpublished.
    Stires62 · 3 years ago
    Good Morning,

    Is there anyway to prevent a user from right clicking "view code" and seeing the admins password?
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @smith123451 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
  • To post as a guest, your comment is unpublished.
    smith123451 · 3 years ago
    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!
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @jacjas 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
  • To post as a guest, your comment is unpublished.
    jacjas · 3 years ago
    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
  • To post as a guest, your comment is unpublished.
    jackie · 3 years ago
    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
  • To post as a guest, your comment is unpublished.
    oh yeah · 3 years ago
    is it have to coding?