+ Reply to Thread
Results 1 to 2 of 2

Using VBA to lock / unlock rows - error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-18-2018
    Location
    Amsterdam, Holland
    MS-Off Ver
    2013
    Posts
    104

    Using VBA to lock / unlock rows - error

    Hello all,

    In attached file i use the following vba code to lock / unlock rows;

    When data is entered in column "J" the row locks. When i remove the data in column J the row unlocks.

    Private Sub Worksheet_Change(ByVal Target As Range)
      If Not Intersect(Target, Columns("A:I")) Is Nothing And Target.CountLarge > 0 Then
        If Len(Cells(Target.Row, "J").Value) Then
          MsgBox "When you closed the row with your initials you cannot change values in columns A : I." & _
                 vbLf & vbLf & "Remove your initials if you are sure you want to change data.", vbExclamation
                 
          Application.EnableEvents = False
          Application.Undo
          Application.EnableEvents = True
        End If
      End If
    End Sub
    It works perfect except for 1 thing;

    When i remove the sign to unlock the row i'm unable to delete the row.
    See example;

    Row 3 is not locked anymore but i can't delete the row. The code fires and the data returns.

    Someone knows what's wrong?
    Attached Files Attached Files

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: Using VBA to lock / unlock rows - error

    .
    Here is my take on it ....

    It appears as long as the row (in your example) that contains the term "locked" in Col J doesn't move up to replace the row immediately above it ... the macro works as you
    intend.

    Example : remove the term "locked" from row 2. Now delete row 2 only. The macro works as desired.

    Now, attempt to delete the new row 2. You can't ... because the term "locked" is moving into what used to be row 2 / col J that was empty. You are changing the condition of Col J in row 2 ... so the macro
    activates. Hope that is understandable.

    Although it isn't how you want it .... you'll need to remove any data from Col J that is directly below the row you are wanting to delete.

    I would presume you could change your macro to include an IF / ELSE statement that if the OFFSET of Col J directly below the row/cell J you are attempting to delete is NOT EMPTY, to allow
    that row to be deleted.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to lock rows with value in a corresponding cell while unlock the other rows
    By wowow in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-06-2016, 10:57 PM
  2. [SOLVED] Lock and Unlock several cells when a value is given
    By Mr_Focal in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-26-2015, 11:33 AM
  3. Lock Unlock Row
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-28-2012, 03:57 PM
  4. lock/unlock rows based on values in cell
    By aravindhan_31 in forum Excel General
    Replies: 5
    Last Post: 06-14-2012, 03:33 PM
  5. Lock and Unlock Cells with VBA
    By gelinasc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2012, 09:40 PM
  6. MAcro to lock and unlock cells depending on rows selected.
    By ballack in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2007, 11:04 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1