Consulting

Results 1 to 4 of 4

Thread: Protect a Worksheet except while macro is running

  1. #1
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location

    Protect a Worksheet except while macro is running

    Hi,

    I would like to create a worksheet with some unlocked cells for the user to put inputs, and then launch a macro that writes the outputs in some other cells. I would like to lock the cells containing the outputs. How can I unprotect the sheet in my macro, and protect it again afterwards? Note that the user won't have the password to unlock the cells.

    I hope I was clear enough.

    Jungix

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Jungix,

    While you could protect the sheet on macro start and unprotect when it is complete, you could also just protect it setting the UserInterfactOnly argument to true. That keeps it unprotected for macros (while the workbook is open, it needs to be set again when closed/reopened) but protected to the user.
    [vba]Sheets("Name").Protect Password:="abc",UserInterfaceOnly:=True[/vba]
    Look in the VBA help at the protect method for more details.
    Matt

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    .. but here's the unprotect version anyway!
    [VBA]
    ActiveSheet.Unprotect Password:="test"
    ActiveCell.FormulaR1C1 = "Testing"
    ActiveSheet.Protect Password:="test", DrawingObjects:=True, Contents:=True, Scenarios:=True

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Thanks!

    I didn't think of putting the password in my code itself. What a dumb I am!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •