+ Reply to Thread
Results 1 to 15 of 15

Disable Cells based on Cell Selection

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Disable Cells based on Cell Selection

    I've seen lots of posts about how to disable a cell based on the content of another cell, however I want to disable/re-enable cells based on the user clicking/selecting another cell. The range of affected cells are in this case, C12:C15 (these are the cells the user can select) and C18:C27 (these are the cells to be disabled/re-enabled).

    For example, if the user clicks

    Cell C12 it should disable Cells C19:C27
    Cell C13 it should disable Cells C18 and C22:C27 and re-enable C19:C21
    Cell C14 it should disable Cells C18:C21 and C25:C27 and re-enable C22:C24
    Cell C15 it should disable Cells C18:C24 and re-enable C25:C27

    Is this possible to do?

    Ideally, I would like the disabled cells to be 'greyed out' as well, would this have to be done using 'conditional formatting' or could it be achieved as part of any code?

    Many thanks

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Disable Cells based on Cell Selection

    HangMan,

    In the appropriate worksheet code module:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Disable Cells based on Cell Selection

    Many thanks tigeravatar... I'm a bit new to VBA, but copying and pasting your code into the worksheet module is giving be the following compile error (though I don't see why).

    Ambiguous name detected: Worksheet_SelectionChange

    Also, the code at the start, 'Const strPW As String = "abc123"' is added to the end of the previous code, i.e. above the line break, if that makes any sense?

    Am I doing something wrong?

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Disable Cells based on Cell Selection

    HangMan,

    That means you already have an existing Worksheet_SelectionChange event code (look for another Sub in your code that is also named Worksheet_SelectionChange). You'll either need to merge it with what I've provided, or replace it with what I've provided.

    The 'Const strPW' line should be at the top of the code module, so just cut and paste it to the very top. Change abc123 to the actual sheet password used. If there is no sheet password used, remove all the text between the quotes so that it becomes:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Disable Cells based on Cell Selection

    Okay, that works perfectly... How would I edit the code so that the worksheet isn't protected each time one of the cells is selected. I would like the user to be able to freely click on any cell within the range C12:C15 and for the relevant cells to lock/unlock without needing to unprotect the sheet each time? Or am I missing something else?

    Many thanks

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Disable Cells based on Cell Selection

    As far as I'm aware, the only way to prevent a user from selecting a cell is to lock it, and then protect the sheet with the stipulation that only unlocked cells can be selected. So to change a cell's status from locked to unlocked and vice versa is to unprotect the sheet, perform the operation, and then protect it again.

  7. #7
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Disable Cells based on Cell Selection

    Okay, that makes sense... I may need to rethink my approach slightly...

    Many thanks for your very quick response and thank you for all your help and patience...

  8. #8
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Disable Cells based on Cell Selection

    Okay, my worksheet has several grouped rows and I want to be able to protect the worksheet, so to take this further, what I would like to be able to do is for the user to be able is to click a toggle button that shows/hides the groups and in doing so for the sheet to become temporarily unlocked, to allow the row range to expand or contract (as this isn't possible when the sheet is protected) and once this action is complete, for the sheet to then be locked again.

    How would I go about achieving this?

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Disable Cells based on Cell Selection

    Generally speaking, if you want a sheet to be locked except for specific interaction/input, I'd go with a userform. Userforms are powerful tools that can be designed to meet specific requirements, and made user-friendly. You can control what a user can/can't do on a userform to prevent mistakes or invalid entries.

    Unfortunately, there is no sheet event to detect the expansion/collapse of a group, so you can't capture that event in code. You can programatically expand/collapse groups with code similar to below. This code toggles whether a group is shown or hidden. Rows(2) represents the first (top) row in the group:
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Disable Cells based on Cell Selection

    Hi tigeravatar,

    This works perfectly for my needs, thank you. I take on board your comments about using a userform, though in this case, that isn't going to be possible.
    I applied your code to a shape using a macro and it works there as well. If I wanted the text in the shape to toggle to say, "Show", '"Hide", how would I adapt your code to achieve this?

  11. #11
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Disable Cells based on Cell Selection

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Disable Cells based on Cell Selection

    Hi tigeravatar,

    So, I amended the code to this. My shape is called Rectangle1. But it errors on .Text = "Show". Am I doing something wrong?

    Please Login or Register  to view this content.

  13. #13
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Disable Cells based on Cell Selection

    Oh right, sheet needs to be unprotected to change the text. You'll need to move the ActiveSheet.Unprotect line to near the top:
    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Disable Cells based on Cell Selection

    Ah, okay, that makes sense and it now works perfectly.

    Many thanks for your help, it is greatly appreciated.

  15. #15
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Disable Cells based on Cell Selection

    You're very welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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