+ Reply to Thread
Results 1 to 12 of 12

Prevent changing value in cell

  1. #1
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Prevent changing value in cell

    I want to allow users to select a cell in a column, but not allow them to change the contents of the cell.
    Also, don't want to allow them to select all the cells on the sheet.

    I'm guessing that I need to use the Change event, and restore the contents back to what they were before the change.

    Is there a better way to do this?
    Last edited by foxguy; 06-16-2009 at 01:19 PM.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Prevent changing value in cell

    Hi Foxguy,

    Is worksheet protection not an option? That's kind of the entire point behind worksheet protection - allow users to edit what you want them to edit, and lock what they shouldn't. You can even set it so that they can't select locked cells when you're adding the worksheet protection.

    Right-click on the cells you want users to edit, choose Format Cells. On the Protection tab un-check 'Locked'. Protect the worksheet, and near the top of the list of allowable options, un-check 'Select Locked cells'.

    That should do it.

  3. #3
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Prevent changing value in cell

    Paul, I have a situation, where I want to allow the users to select special cells that they cannot edit, but I don't want to allow ALL locked cells to be selected.
    So I have to unlock those special cells with the sheet protected, but I want to prevent them from editing those cells.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Prevent changing value in cell

    In which case I suspect you may need to use a Worksheet_Change event, ie:

    a) unprotect all cells that can be selected
    b) protect sheet and set such that protected cells can NOT be selected
    c) install the below into the Sheet Object on which the cells reside (revising range as required):

    Please Login or Register  to view this content.
    In the above cells B4, B7 & B9 can all be selected but if they are altered the action is reversed and user notified as such.

    Obviously this then requires users have enabled macros in the first instance etc...

  5. #5
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Prevent changing value in cell

    DonkeyOte;

    I read this in my help:

    Please Login or Register  to view this content.
    I haven't tested it yet, but will your macro still work, since the Undo command is not the first line in the macro?

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Prevent changing value in cell

    Why not test it and see...

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Prevent changing value in cell

    It does work just fine. But since the help file says it shouldn't, should I be concerned about some unforseen side effect?

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Prevent changing value in cell

    No.... I can't think of a good way to put it but the help file is essentially making the point that you should run the Undo call prior to performing any other "recordable" actions... in the code provided no such actions are performed prior to the Undo being called.

  9. #9
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Prevent changing value in cell

    One last question.

    In my database programming, they warn us that if we discover an undocumented feature, we should not count on it being available in future updates. Do you think there is any chance that this might disappear in the future?

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Prevent changing value in cell

    No, I can't see MS removing the Undo feature, can you ?

    Note: this isn't undocumented it's a case of interpretating the narrative in the help file, as I say the key is to call the Undo before any further "recordable" actions are undertaken else the wrong action will be undone...

    The reason we disable Application Events prior to calling the Undo action is that if we didn't, given we're in a Change event, we'd end up in a perpetual loop... the Undo would obviously change the sheet which would in turn invoke the change event which would in turn invoke the Undo which would in turn invoke the change event which would in turn...

  11. #11
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Prevent changing value in cell

    I didn't mean "would the Undo feature be removed".
    I meant, "do you think that MS will change the way it works"? The undocumented feature is that Undo will work on any line in a macro, not just the 1st line.

    It sounds like you think that it will always work the way it currently does, and I agree with you.

  12. #12
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Prevent changing value in cell

    In which case I suspect you may need to use a Worksheet_Change event, ie:

    a) unprotect all cells that can be selected
    b) protect sheet and set such that protected cells can NOT be selected
    c) install the below into the Sheet Object on which the cells reside (revising range as required):

    Please Login or Register  to view this content.
    In the above cells B4, B7 & B9 can all be selected but if they are altered the action is reversed and user notified as such.

    Obviously this then requires users have enabled macros in the first instance etc...
    This is very nice!
    Last edited by cmore; 06-30-2014 at 03:47 AM.

+ 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