+ Reply to Thread
Results 1 to 4 of 4

Trying to use VB to protect and Unprotect a worksheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Holland, MI
    MS-Off Ver
    Excel 2007
    Posts
    110

    Trying to use VB to protect and Unprotect a worksheet

    Hi All,
    How do I protect a specific worksheet (not necessarily the "active sheet") with VB. That particular sheet, I initially protected it using "password" the password. Then I'm trying to unprotect the sheet when the userform is initialized. Then when it's terminated, I want to re-protect it. Here is the code I'm trying to use...

    To Unprotect
    Private Sub UserForm_Initialize()
    Dim PW As String
    PW = "password"
    Worksheets("Summary").Unprotect Password: PW
    End Sub
    To Protect (haven't really tested this one, just assuming it's wrong because the other doesn't work.
    Private Sub UserForm_Terminate()
    Dim PW As String
    PW = "password"
    Worksheets("Summary").protect Password: PW
    End Sub
    Can someone help me figure out the correct code? Thanks!

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,046

    Re: Trying to use VB to protect and Unprotect a worksheet

    Try:
    Private Sub UserForm_Initialize()
    Worksheets("Summary").Unprotect Password:="password"
    End Sub
    
    Private Sub UserForm_Terminate()
    Worksheets("Summary").Protect Password:="password"
    End Sub

  3. #3
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Holland, MI
    MS-Off Ver
    Excel 2007
    Posts
    110

    Re: Trying to use VB to protect and Unprotect a worksheet

    That worked. Thanks!

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,046

    Re: Trying to use VB to protect and Unprotect a worksheet

    My pleasure.

+ 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