+ Reply to Thread
Results 1 to 7 of 7

Locking and unlocking all sheets with a password

  1. #1
    Registered User
    Join Date
    01-16-2019
    Location
    Didam, Netherlands
    MS-Off Ver
    2013
    Posts
    4

    Locking and unlocking all sheets with a password

    Hi there!

    I am trying (as a noob) to build some code which allows me to lock/unlock all sheets in my workbook with a password. I am using the following code:

    Sub Lock()
    pass = InputBox("Enter Pass")
    For Each Worksheet In ActiveWorkbook.Worksheets
    Worksheet.Protect Password:=pass
    Next
    Sheets("Totaal").Select
    Range("L4").Select
    With Selection.Font
    .Color = -11489280
    .TintAndShade = 0
    End With
    ActiveCell.FormulaR1C1 = "File is locked"
    Exit Sub
    End Sub

    Sub Unlock()
    On Error GoTo booboo
    unpass = InputBox("Enter Pass")
    For Each Worksheet In ActiveWorkbook.Worksheets
    Worksheet.Unprotect Password:=unpass
    Next
    Sheets("Totaal").Select
    Range("L4").Select
    With Selection.Font
    .Color = -16776961
    .TintAndShade = 0
    End With
    ActiveCell.FormulaR1C1 = "File is unlocked"
    Exit Sub
    booboo: MsgBox "Wrong password"
    End Sub
    This gives me an error in the line that is highlighted above. I don't see what the problem is. The Unlock Sub seems to work...Any help WILL be appreciated!

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Locking and unlocking all sheets with a password

    Hi and welcome

    You're attempting to change the font colour AFTER the sheet has been protected.

    Change this line:
    Please Login or Register  to view this content.
    To this:
    Please Login or Register  to view this content.
    BSB

  3. #3
    Registered User
    Join Date
    01-16-2019
    Location
    Didam, Netherlands
    MS-Off Ver
    2013
    Posts
    4

    Re: Locking and unlocking all sheets with a password

    Thank you for helping. This however didn't solve my problem yet. It did give me an idea where to look. Your suggestion gave me the same error:

    Error.jpg

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Locking and unlocking all sheets with a password

    I mocked up a file and it worked fine for me.

    Could you attach a desensitized version of your file?

    BSB

  5. #5
    Registered User
    Join Date
    01-16-2019
    Location
    Didam, Netherlands
    MS-Off Ver
    2013
    Posts
    4

    Re: Locking and unlocking all sheets with a password

    I don't know why, but it is working now. Thanks a lot for helping! You're the hero of the day for me!

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Locking and unlocking all sheets with a password

    Happy to help

    BSB

  7. #7
    Registered User
    Join Date
    01-16-2019
    Location
    Didam, Netherlands
    MS-Off Ver
    2013
    Posts
    4

    Re: Locking and unlocking all sheets with a password

    Nevermind, problem solved.
    Attached Images Attached Images
    Last edited by wyrm76; 01-16-2019 at 10:01 AM.

+ 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. Locking and unlocking of cells
    By srinivassurapareddi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 02:19 AM
  2. locking or unlocking with macro
    By allgeef in forum Excel General
    Replies: 3
    Last Post: 04-03-2011, 09:44 PM
  3. Locking/Unlocking all Sheets in Workbook
    By ACM2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-26-2010, 05:46 PM
  4. Locking and Unlocking Cells
    By vikas.bhandari in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2010, 10:27 AM
  5. Replies: 2
    Last Post: 12-04-2008, 08:06 PM
  6. locking and unlocking worksheets
    By aci1984 in forum Excel General
    Replies: 2
    Last Post: 06-18-2007, 12:01 PM
  7. Locking/Unlocking sheets using a range.
    By wilro85 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-17-2006, 05:50 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