+ Reply to Thread
Results 1 to 4 of 4

VBA Protect/Unprotect worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    12-03-2013
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2013
    Posts
    2

    VBA Protect/Unprotect worksheet

    Hi

    I have a small problem.

    In my vba code I use the following.

    ActiveSheet.Unprotect Password = "1234" and
    ActiveSheet.Protect Password = "1234"

    Which Works just fine.

    But when I want to unprotect manually it says the password is incorrrect.

    Any idea why?

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: VBA Protect/Unprotect worksheet

    This is because your password is really "Falsk". The code is incorrect - it passes an expression rather than a named argument. Change the code to this:
    ActiveSheet.Unprotect Password:="1234"
    ActiveSheet.Protect Password:="1234"

  3. #3
    Forum Contributor Toonies's Avatar
    Join Date
    07-30-2009
    Location
    Newcastle, UK
    MS-Off Ver
    Excel 2016
    Posts
    506

    Re: VBA Protect/Unprotect worksheet

    Try this VBA

    place in a Module

    ' Why protect every sheet when you might only need to do something with one sheet?
    'Sub ProtectSh()
    Sub ProtectSh(Optional wSheet As Worksheet)
        If wSheet Is Nothing Then
            ' Best to be clear about which workbook you are protecting:
            'For Each wSheet In Worksheets
            For Each wSheet In ThisWorkbook.Worksheets
                ' Adding userinterfaceonly:=true means that your VBA code can do whatever it
                '   needs to without having to unprotect the sheet first!
                'wSheet.Protect Password:="Password"
                wSheet.Protect Password:="Password", userinterfaceonly:=True
            Next wSheet
        Else
            wSheet.Protect Password:="Password", userinterfaceonly:=True
        End If
    End Sub
    
    ' Why unprotect every sheet when you might only need to do something with one sheet?
    'Sub UnprotectSh()
    Sub UnprotectSh(Optional wSheet As Worksheet)
        If wSheet Is Nothing Then
            For Each wSheet In Worksheets
                wSheet.Unprotect Password:="Password"
            Next wSheet
        Else
            wSheet.Unprotect Password:="Password"
        End If
    End Sub

    Then place this in your sheet
    Call UnprotectSh
    
    Your Code!!
    
    Call ProtectSh

  4. #4
    Registered User
    Join Date
    12-03-2013
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: VBA Protect/Unprotect worksheet

    Thanks a lot, that solved my problem.

+ 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] Trying to use VB to protect and Unprotect a worksheet
    By jfoerch in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-09-2013, 09:44 AM
  2. Protect and Unprotect Worksheet VBA
    By s45yth in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2011, 07:20 PM
  3. Protect/Unprotect worksheet
    By mab in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2010, 06:21 PM
  4. How to protect and unprotect worksheet
    By ballack in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-21-2007, 08:35 AM
  5. [SOLVED] protect/unprotect worksheet
    By Alex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-09-2005, 02:05 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