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. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,837
    Location
    try this

    Also did a little formatting on the MsgBox

    Option Explicit
    
    
    'I assume that the entire WS is Locked = False
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cl As Range
        Dim check As VbMsgBoxResult
    
    
        For Each cl In Target
            With cl
                If Len(.Value) > 0 Then
                
                    check = 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 check = 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 cl
    
    
        
    End Sub
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Paul_Hossler; 05-13-2022 at 07:56 PM.
    ---------------------------------------------------------------------------------------------------------------------

    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
  •