+ Reply to Thread
Results 1 to 9 of 9

Lock a range of cells based off a value of another cell and duplicate for multiple rows

  1. #1
    Registered User
    Join Date
    08-07-2019
    Location
    New York, New York
    MS-Off Ver
    Professional Plus 2016
    Posts
    7

    Lock a range of cells based off a value of another cell and duplicate for multiple rows

    Hi,

    I have been having trouble with this problem.

    I am creating an assessment and when the value in Column K is equal to "No" I would like for Columns L through T and X & Y to be locked. I have been able to get it to work for one row but I have 500 rows and am hoping there is a way to not have to write out the macro 500+ times.

    This is what I have for 1 row and range right now:

    Private Sub Worksheet_Change(ByVal Target As Range)

    If Range("K3") = "Yes" Then
    Range("L3:T3").Locked = False
    ElseIf Range("K3") = "No" Then
    Range("L3:T3").Locked = True


    End If

    End Sub

    I need it for all the rows in my sheet. For an example: If K3 is "no" then only L3:T3 and X3:Y:3 will be locked and if K4 is "Yes" then L4:T4 and X4:Y:4 are unlocked but if K5 is "No" then L5:T5 and X5:Y:5 will be locked, and so forth...

    Any help is appreciated!

    Thank you

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    How do cells in column K change. Are they manually changed or do they contain formulae that change as a result of some precedent cells elsewhere?

    If they're manually changed then


    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    08-07-2019
    Location
    New York, New York
    MS-Off Ver
    Professional Plus 2016
    Posts
    7

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    This worked, thank you!

    It is selected through a dropdown from a data validation

    I am not having a different problem with this now. I have a button that clears all the user inputted data, however it is giving me a mismatch error now. (I think it may be because the cells are locked?) Any idea of how to get around that as well.

    This is the code I have for that button:

    Sub CommandButton1_Click()

    If MsgBox("This will clear all user inputted data, these changes cannot be undone. Do you wish to proceed?", vbYesNo + vbQuestion) = vbYes Then

    Worksheets("02_Risk Assessment").Unprotect Password:="abc"
    Worksheets("04_Control Assessment").Unprotect Password:="abc"

    Worksheets("02_Risk Assessment").Range("E3:J50").ClearContents
    Worksheets("02_Risk Assessment").Range("L3:L50").ClearContents
    Worksheets("04_Control Assessment").Range("K3:T480").ClearContents
    Worksheets("04_Control Assessment").Range("X3:Y480").ClearContents

    Worksheets("02_Risk Assessment").Protect Password:="abc"
    Worksheets("04_Control Assessment").Protect Password:="abc"


    End If

    End Sub

    The formula that you gave previously is in the "04_Control Assessment" sheet.

    Thank you!

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    Hi,

    If a sheet is unprotected then whether a cell is locked or not is irrelevant. A locked cell is only prevented from being changed when sheet protection is in place.

    Please upload the workbook so that we can establish the problem.

    Incidentally, and for reference. In VBA rather than use the sheet tab name - which is too easily changed by a user and would cause a macro to fail, use the VBA Sheet code name. So instead of

    Worksheets("04_Control Assessment")
    use
    Sheet4.Range("blah blah ' assuming 'Sheet4' is indeed the VBA code name.

  5. #5
    Registered User
    Join Date
    08-07-2019
    Location
    New York, New York
    MS-Off Ver
    Professional Plus 2016
    Posts
    7

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    Attached is the spreadsheet. Thank you
    Attached Files Attached Files

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    Hi,

    In the 04 Assessment sheet you are testing for the Target being "Yes".
    In a sheet change event the target is the cell that's being changed.

    When you click the button on the Information sheet, that clears the contents in K3:T480 on the 04 sheet but at that time the target is the whole K:T range and hence when the sheet change event comes to the check for the target = "Yes" it doesn't understand what K:T = "Yes" means

    i.e. there are two events that change the sheet and hence the sheet change macro. One where you manually change a cell in column K, and the other when another macro clears the contents of a range.

    One way round this would be to trap the error and ignore it. So use

    Please Login or Register  to view this content.
    The On Error Goto 0 cancels the On error Resume trap since it's not usually wise to let code bypass errors without undertsanding why an error might have occurred.
    Last edited by Richard Buttrey; 08-08-2019 at 12:16 PM.

  7. #7
    Registered User
    Join Date
    08-07-2019
    Location
    New York, New York
    MS-Off Ver
    Professional Plus 2016
    Posts
    7

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    Hi,

    Where do I put this code. I tried putting it into the button on the information sheet and that by passes the code but does not perform the clear to the 04 Assessment tab.

    Code:

    Please Login or Register  to view this content.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    No, It's the sheet change event for the 04 Assessment sheet.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-07-2019
    Location
    New York, New York
    MS-Off Ver
    Professional Plus 2016
    Posts
    7

    Re: Lock a range of cells based off a value of another cell and duplicate for multiple row

    That was then giving me Error 1004 saying the sheet is protected event tho the code is there to first unprotect.

    However, I found a way to make it work by also adding it into the button, the code for the 04 Assessment and button are below.

    Thank you again for your help!

    Please Login or Register  to view this content.




    Please Login or Register  to view this content.

+ 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] Lock/Unlock range cells based on text value in other range cells
    By boboivan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-12-2014, 12:50 PM
  2. [SOLVED] First unhide all rows - then hide rows based on specific cell value for a range of cells
    By robbiekh in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-22-2013, 05:46 PM
  3. [SOLVED] Delete rows based on duplicate cell, but leaving first and last duplicate.
    By LadyNicole in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2013, 05:07 AM
  4. Conditionally lock multiple cells in a row based on one cell value.
    By DavidPAR in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 02:10 PM
  5. Conditional Formatting - Grey Out and Lock A range based on a cell value in that range
    By Excelgnome in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2012, 07:31 PM
  6. How to lock and unlock a range of cells based on certain cell's value?
    By potato in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-25-2012, 04:34 AM

Tags for this Thread

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