Results 1 to 5 of 5

Thread: 2016 Excel VBA Code for Locking a Cell after Data Entry

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,837
    Location
    Implemented via RightClick

    Could probably use some more error checking


    Option Explicit
    
    
    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
        Dim rCell As Range
        Dim ans As VbMsgBoxResult
    
    
        For Each rCell In Target.Cells
            With rCell
                If Len(.Value) > 0 Then
                
                    ans = MsgBox("Do you want to reset this cell?" & vbCrLf & vbCrLf & _
                        vbTab & .Value & "  (" & .Address(False, False) & ")", vbYesNo, "Cell Lock Notification")
                    
                    If ans = vbYes Then
                        If ActiveSheet.ProtectContents Then ActiveSheet.Unprotect Password:="123" '   unprotect first
                        Application.EnableEvents = False
                        .ClearContents
                        .Locked = False
                        Application.EnableEvents = True
                        ActiveSheet.Protect Password:="123"
                    End If
                End If
            End With
        Next
            
        Cancel = True
        
    End Sub
    
    
    'I assume that the entire WS is Locked = False
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rCell As Range
        Dim ans As VbMsgBoxResult
    
    
        For Each rCell In Target
            With rCell
                If Len(.Value) > 0 Then
                
                    ans = MsgBox("Is this entry correct?" & vbCrLf & vbCrLf & _
                        vbTab & .Value & "  (" & .Address(False, False) & ")" & vbCrLf & vbCrLf & _
                        "This cell cannot be edited after entering a value.", vbYesNo, "Cell Lock Notification")
                    
                    If ans = vbYes Then
                        If Me.ProtectContents Then Me.Unprotect Password:="123" '   unprotect first
                        .Locked = True
                        Me.Protect Password:="123" '   not Activeshet.
                    
                    Else
                        .ClearContents
                        ActiveCell.Offset(-1, 0).Select '   reselect data entry cell
                    End If
                End If
            End With
        Next rCell
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •