+ Reply to Thread
Results 1 to 27 of 27

Lock cell based on value in another cell

  1. #1
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Lock cell based on value in another cell

    So, I've watched several videos on this matter but I still can't seem to make it work. I'm not sure if it's the preconditions that are causing the confusion or something else. I'll try to be as specific as possible with what I'm trying to achieve. The target is the PLANNER sheet.

    If there's text in Y6, then unlock Y7, else lock Y7. Y7 is a drop down list with pre-set text choices. If user is trying to access Y7 return message box saying "Please make a choice in the cell above first."
    If there's text in Y7, then unlock Y8, else lock Y8. Y8 is a drop down list with pre-set text choices. If user is trying to access Y8 return message box saying "Please make a choice in the cell above first."
    If there's text in Y8, then unlock Y9, else lock Y9. Y9 is a drop down list with pre-set text choices. If user is trying to access Y9 return message box saying "Please make a choice in the cell above first."
    If there's text in Y9, then unlock Y10, else lock Y10. Y10 is a drop down list with pre-set text choices. If user is trying to access Y10 return message box saying "Please make a choice in the cell above first."

    If the user makes a choice in Y7 which unlocks Y8 and later decides to delete the choice from Y7 which emptys the cell, I wan't Y8 to change from unlocked to locked automatically.
    If the user makes a choice in Y8 which unlocks Y9 and later decides to delete the choice from Y8 which emptys the cell, I wan't Y9 to change from unlocked to locked automatically.
    If the user makes a choice in Y9 which unlocks Y10 and later decides to delete the choice from Y9 which emptys the cell, I wan't Y10 to change from unlocked to locked automatically.

    If the user makes choices for cells Y7:Y10 and decides to delete the information only in Y7, I want Y8:Y10 automatically to become empty as well and locked in addition.
    If choices were made in Y7:Y10 and the user decides to delete the information only in Y8, I want Y9:Y10 automatically to become empty as well and locked in addition.
    If choices were made for Y7:Y10 and the user decides to delete the information only in Y9, I want Y10 to automatically become empty as well and locked in adition.

    I want this to apply only to the certain sheet I have in mind and not the others in the workbook. In addition, the user has the choice to copy this whole sheet into another sheet if he/she wishes. So I want this code to apply to the newly copied sheet as well.

    Preconditions:
    Upon opening the file Y6 is unlocked and Y7:Y10 are locked with a password (the whole sheet is password protected).
    Cells Y7:Y10 are drop down lists with pre-set texts. There are 15 choices to make.

    It sounded easy at first, but upon trying several different set ups, I can't seem to make it work. I've tried several ByVal Target As Range codes (Change/SelectionChange) but I can't seem to make it work and now I'm really confused.
    Attached Files Attached Files
    Last edited by Arphaxad; 06-29-2020 at 04:25 AM.

  2. #2
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    I believe I just wrote the easiest and most amateur-ish piece of code, but it seems to work. What do the experts say?

    Please Login or Register  to view this content.
    If that is OK, is there any possibility to deny users access to the developer/macro part of the file as the passwords will be visible there (both in the module and worksheet tabs)?

    EDIT: This code seems to have slowed down the file considerably. I suspect it has to do with the combination of SelectionChange and the fact that it's constantly implementing the password, but I might be wrong. Suggestions?
    Last edited by Arphaxad; 06-29-2020 at 05:02 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Lock cell based on value in another cell

    Below code is not exactly what you want,
    but maybe you can have a look on how it work then modify it according to your need.

    Pre-condition :
    Cell Y6:Y10 is not locked / not password protected.

    Please Login or Register  to view this content.
    From the code above, there is no lock/unlock process, but something like "disabling" :
    The user won't be able to fill whatever cell (between Y7:Y10) if the cell above it is blank.

    Please Login or Register  to view this content.
    When the user clear any cell within cell Y7:Y10, the code will clear the cell below it until Y10.
    Last edited by karmapala; 06-29-2020 at 06:31 AM.

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

    Re: Lock cell based on value in another cell

    Place this macro in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macro into the empty window that opens up. Close the window to return to your sheet. Save the file, close it and then re-open it.
    Please Login or Register  to view this content.
    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.

  5. #5
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33
    Quote Originally Posted by karmapala View Post
    Below code is not exactly what you want,
    Thank you, mate. Interesting solution. It worked when I tried it yesterday. Added a column to the sheet today, and it seems to have messed with some functions/macros as it's not working properly anymore eventhough I revised the code to accommodate for the added column. Can't remember the specific error. I'll have to check more into detail tomorrow and get back. Until then, thanks again.
    Last edited by AliGW; 06-30-2020 at 11:44 AM. Reason: Please don't quote unnecessarily!

  6. #6
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33
    Quote Originally Posted by Mumps1 View Post
    Place this macro in the code module for ThisWorkbook.
    Thanks as always, mate. But this solution seems to come into conflict with another problem you solved for me the other day, namely the CLEAR button as I'm not able to use it now because it says that the data it's trying to clear (Y6:Y10) is in a password protected range.

    Also, for some reason, having added a column messed up the CLEAR button as well eventhough I revised the code. I added a column before column Y, so I revised all macros for columns after column Y. But apparently thst didn't help.
    Last edited by Arphaxad; 06-30-2020 at 11:56 AM.

  7. #7
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Try the attached version.

  8. #8
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Thanks, mate. But upon clicking on the CLEAR button, it seems to mess with the format in Z6:Z10. It works perfect until I use the CLEAR button. After that, the cell Z6 locks and I can't make any choice at all in Z6:Z10. Maybe always leaving Z6 unlocked since the first choice will always go in there and thus it needs to be open all the time?
    Last edited by Arphaxad; 07-01-2020 at 04:34 AM.

  9. #9
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Replace the current "Clear" macro with this one:
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Quote Originally Posted by Mumps1 View Post
    Replace the current "Clear" macro with this one:
    Please Login or Register  to view this content.
    Thanks. That seems to get the job done. Only "problem" now is that these changes seem to disable the locking/protecting COPIED or SAVED sheets. As far as my untrained eye can see, the macros for the COPY and SAVE buttons do include a line of code respectively where new COPIED or SAVED sheets should be password protected. But upon clicking either of those buttons, the newly created sheets seem to come out unprotected.
    Attached Files Attached Files

  11. #11
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Try these revised macros:
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Seems to work like a sharm. You're a savage, mate. I'll do some testing, but from what I can see, it seems to work just fine. Thanks again! I'll need to study the revisions you've made in order to learn.

  13. #13
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    You are very welcome.

  14. #14
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Quote Originally Posted by Mumps1 View Post
    You are very welcome.
    I discovered a minor issue today. If I click the COPY button the pop-up message box asks me how many copies I want, as it should. If I chose two copies, there's no issue. But if I chose only one copy and then click on the COPY button again in order to generate an additional copy, I get a error message stating that there's already a copy/sheet with that name in existence. I'm suspecting the macro doesn't recognize that it has already created a sheet with the name "PLANNER-1" and thus tries to name it 1 instead of 2 since I clicked the button on two separate occassions. As I said, if I chose two copies on the first click, it works as it creates "PLANNER-1" and "PLANNER-2". But not if I do it on two separate occasions.

  15. #15
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Try the attached file.
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33
    Quote Originally Posted by Mumps1 View Post
    Try the attached file.
    Woah, you're fast, mate. Care to let me know what changes you've made? I've updated the file with new features since last time.

  17. #17
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    I made some changes to both the Save and Copy buttons. If you have changed the file, depending on what those changes were, the macros may not work properly now. You would have to attach a copy of your revised file for me to use as a test.

  18. #18
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Quote Originally Posted by Mumps1 View Post
    I made some changes to both the Save and Copy buttons. If you have changed the file, depending on what those changes were, the macros may not work properly now. You would have to attach a copy of your revised file for me to use as a test.
    Not sure if the revisions in question will affect the macros, but see attached file. I really feel bad about bothering you with this again. Appreciate your help, mate.

  19. #19
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    I tested the macros I used in the file in Post #15 and they worked properly in your revised file.

  20. #20
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Quote Originally Posted by Mumps1 View Post
    I tested the macros I used in the file in Post #15 and they worked properly in your revised file.
    Thanks mate. Sorry for the late reply, but I've run some tests and everything seems to check out.
    Although, do you have any idea why the radar chart doesn't copy when I use the SAVE button?
    Attached Files Attached Files

  21. #21
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Try the attached file.

  22. #22
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Quote Originally Posted by Mumps1 View Post
    Try the attached file.
    Thanks. Works like a charm. Just realised I attached an outdated version of the file. I've tried to implement your changes to my most recent file, but it seems as if the chart pastes to the wrong area. I've tried to change it to AC3:AF21, but it still seems to misplace when I SAVE the sheet. It copies the chart to AB2:AF18 instead of AC3:AF20.
    Last edited by Arphaxad; 07-14-2020 at 04:17 AM.

  23. #23
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Replace this line of code in both the "Save" and "Copy" macros (two occurrences in each macro)
    Please Login or Register  to view this content.
    with this line:
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Quote Originally Posted by Mumps1 View Post
    Replace this line of code in both the "Save" and "Copy" macros (two occurrences in each macro)
    Please Login or Register  to view this content.
    with this line:
    Please Login or Register  to view this content.
    Thanks, but it doesn't work. The SAVE button copies the chart to column AB and not AC. And the COPY button creates two charts now, one which is wrongly placed above one that seems correctly placed.
    Last edited by Arphaxad; 07-15-2020 at 04:11 AM.

  25. #25
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Try the attached file.

  26. #26
    Registered User
    Join Date
    06-22-2020
    Location
    Sweden
    MS-Off Ver
    O365
    Posts
    33

    Re: Lock cell based on value in another cell

    Quote Originally Posted by Mumps1 View Post
    Try the attached file.
    It's pasted in the correct place now. But it's pasted as a Picture with set/fixed/pasted values. This is perfect for the SAVE button. But when I use the COPY button and generate a second PLANNER sheet, I'd like the radar chart to change when I change variables in the model. See PLANNER-1 sheet. When I use the COPY button. I'd like the radar chart to work exactly like in the initial PLANNER sheet.

  27. #27
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Lock cell based on value in another cell

    Try:
    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] VBA code to lock cell based on anotehr cell value not working
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-29-2016, 12:09 PM
  2. Replies: 1
    Last Post: 05-11-2015, 06:02 PM
  3. lock and insert formula in a cell based on selection of text in another cell
    By alirazafazal in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-01-2014, 06:08 AM
  4. Lock/Unlock a cell based on the value selected from drop down in adjacent cell
    By mahesnrm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-23-2013, 08:28 AM
  5. Replies: 1
    Last Post: 09-12-2013, 10:38 PM
  6. Replies: 1
    Last Post: 03-11-2013, 03:10 PM
  7. Replies: 1
    Last Post: 03-10-2013, 01:54 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