+ Reply to Thread
Results 1 to 3 of 3

How to Unprotect and protect cells in a script

  1. #1
    Registered User
    Join Date
    11-17-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    8

    How to Unprotect and protect cells in a script

    I have a script for recording passwords. It has a cell where the user enters a code (cell C3) and the script returns (displays) the password in the adjacent cell (D3). The script also allows a user to click a button to generate a new code for a new password. The new code is displayed in cell C4. To make this script work and keep me from mistakenly changing other fields, I have locked the previous 3 cells (C3, D3 and D4 as well as the password and password code cells (G2:F5000). I then Protect the sheet.

    What I would like to do is to lock JUST C3 to allow a user to enter a code. I thought I could do this with the ActiveSheet.Protect command but I can't seem to get it to work. Here is the code, with my attempt commented out:

    Please Login or Register  to view this content.
    Any help would be appreciated.
    Last edited by jeffreybrown; 10-17-2018 at 06:36 PM. Reason: Added code tags!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: How to Unprotect and protect cells in a script

    I'm a little confused about the sequence that you follow here.
    What I would like to do is to lock JUST C3 to allow a user to enter a code.
    How can the user enter a code in C3 if you lock it? It seems to me that you need to leave C3 and C4 unlocked for the user to do anything.

    Why does this code need to lock C3:G10000 every time it's called? You can do that one time in sheet setup and it won't change.

    If I were doing this here is what I think I would want. First, go to the sheet and lock C3:G10000. Every cell is locked by default anyway so you shouldn't have to do anything. Then unlock C3 and C4 to allow user input. Then the code manages it from there.

    Also you do not need to reference ActiveSheet in a sheet event handler. I would do this:
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-17-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: How to Unprotect and protect cells in a script

    Hi Jeff. Thanks for your reply. Firstly, you are correct, I made a mistake. C3 must NOT be locked. Just a sentence issue, not the way I was thinking.
    For the Script to run, I need to unlock the cells that it will (or could) call so C3, D3 and C4 must be unlocked as well as the data storage range: G2:F10000. C4 needs to be locked for another button script that creates a new password code and writes it to C4.

    I am doing this because I don't want the user (me) to enter data into D3 or C4 (These are both Outputs) or in the data storage range G2:F10000. I hope that clarifies my objective.

+ 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] VB Code to protect unprotect certain cells
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2017, 11:47 PM
  2. Protect/unprotect formula cells of entire workbook
    By shafiqe in forum Excel General
    Replies: 5
    Last Post: 07-14-2013, 01:59 AM
  3. [SOLVED] Protect/Unprotect VBA Script
    By Heat in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-19-2013, 01:03 PM
  4. New to macros - script to protect cells if a particular cell is populated
    By gsandifer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2013, 04:19 PM
  5. Replies: 2
    Last Post: 07-03-2012, 04:42 AM
  6. Macro to unprotect sheet --> unlock cells --> protect again
    By hrhittn23 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-02-2011, 07:00 AM
  7. Unselect locked cells when protect/unprotect worksheet
    By eight8 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-08-2007, 03:00 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