+ Reply to Thread
Results 1 to 8 of 8

Lock cell in column based on value of cell in previous column

Hybrid View

  1. #1
    Registered User
    Join Date
    06-03-2015
    Location
    UK
    MS-Off Ver
    MS Office 15
    Posts
    11

    Lock cell in column based on value of cell in previous column

    Hi there,

    I can see that this question has appeared many times before in slightly different forms, so apologies if I'm repeating a question from before.

    My issue is that I would like to be able to lock the cell in column L, based on a text input in column E. Column E inputs are based on a Data Validated List ("In Development", "Approved", "Complete", "Not Progressing"), so won't vary at all, and column L will only be unlocked if "Not Progressing" is selected (in order to give a reason).

    This should then fill down into every row of the worksheet.

    As a VBA novice, I'm struggling to amend previous script to fit my requirements.

    Greatful for any help provided.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,080

    Re: Lock cell in column based on value of cell in previous column

    Start by unlocking all the cells in your worksheet, protect the worksheet and then try this macro:
     Sub LockCells()
        Application.ScreenUpdating = False
        ActiveSheet.Unprotect
        Dim bottomE As Long
        bottomE = Range("E" & Rows.Count).End(xlUp).Row
        Dim rng As Range
        For Each rng In Range("E2:E" & bottomE)
            If rng = "Not Progressing" Then
                Cells(rng.Row, "L").Locked = True
            Else
                Cells(rng.Row, "L").Locked = False
            End If
        Next rng
        ActiveSheet.Protect
        Application.ScreenUpdating = True
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    06-03-2015
    Location
    UK
    MS-Off Ver
    MS Office 15
    Posts
    11

    Re: Lock cell in column based on value of cell in previous column

    Thanks for the help Mumps1, but this still seems to allow me to edit all of the cells in column L.

    All cells are unlocked, and I have protected the sheet just not with a password.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,080

    Re: Lock cell in column based on value of cell in previous column

    When you run the macro, all cells in column L should be locked where the corresponding cell in column E is "Not Progressing". When I tried it on a dummy file, it worked properly. Can you post a copy of your file to make it easier to find the problem?

  5. #5
    Registered User
    Join Date
    06-03-2015
    Location
    UK
    MS-Off Ver
    MS Office 15
    Posts
    11

    Re: Lock cell in column based on value of cell in previous column

    Thanks - file attached
    Attached Files Attached Files

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,080

    Re: Lock cell in column based on value of cell in previous column

    You can achieve what you want in two ways. 1. If you want to lock column L automatically when you choose "Not Progressing" in the drop down list in column E, you have to copy and paste the following macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Make your selection in column E and the macro will run automatically.
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("E:E")) Is Nothing Then Exit Sub
        Application.ScreenUpdating = False
        ActiveSheet.Unprotect
        If Target = "Not Progressing" Then
            Cells(Target.Row, "L").Locked = True
        Else
            Cells(Target.Row, "L").Locked = False
            End If
        ActiveSheet.Protect
        Application.ScreenUpdating = True
    End Sub
    2. If you want to run the macro manually, use the following macro in a regular module:
    Sub LockCells()
        Application.ScreenUpdating = False
        ActiveSheet.Unprotect
        Dim bottomE As Long
        bottomE = Range("E" & Rows.Count).End(xlUp).Row
        Dim rng As Range
        For Each rng In Range("E5:E" & bottomE)
            If rng = "Not Progressing" Then
                Cells(rng.Row, "L").Locked = True
            Else
                Cells(rng.Row, "L").Locked = False
            End If
        Next rng
        ActiveSheet.Protect
        Application.ScreenUpdating = True
    End Sub
    Use one or the other, not both.

  7. #7
    Registered User
    Join Date
    06-03-2015
    Location
    UK
    MS-Off Ver
    MS Office 15
    Posts
    11

    Re: Lock cell in column based on value of cell in previous column

    This is great - and works perfectly. Thank you so much!

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,080

    Re: Lock cell in column based on value of cell in previous column

    My pleasure.

+ 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. macro to lock cell by column header based on value in another cell by column header
    By Closet Guru in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2015, 12:36 PM
  2. [SOLVED] Lock column based on value in one cell column
    By gilly68 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-23-2012, 05:08 PM
  3. Replies: 2
    Last Post: 05-18-2012, 12:34 AM
  4. Replies: 1
    Last Post: 05-07-2010, 11:23 AM
  5. conditional cell format based on cell in same row, previous column
    By tamiluchi in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-03-2006, 11:15 AM

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