+ Reply to Thread
Results 1 to 8 of 8

Protect/Unprotect Sheets in array with VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Protect/Unprotect Sheets in array with VBA

    Hi all,

    I want to protect or unprotect a number of sheets by using a Macro. I don't want to unprotect ALL sheets, but only certain sheets within my workbook. Below is the code that I have come up with. It does not seem to work yet, can someone help me fix it?

    Private Sub Workbook_Open()
    
    Dim SHEETList As String
    
    SHEETList = " TTL APAC Japan Korea HK Hub China HMT SEA ANZ "
    
    'unlock all sheets in array'
    
    For Each sht In SHEETList
        sht.Unprotect Password:="xxxxx"
    Next sht
    
    End Sub

  2. #2
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Protect/Unprotect Sheets in array with VBA

    This method also does not seem to work...

        With Sheets(Array("TTL APAC", "Japan", "Korea", "HK Hub", "China", "HMT", "ANZ", "SEA"))
        .Protect Password:="xxxxx"
        End With

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,333

    Re: Protect/Unprotect Sheets in array with VBA

    This should do it.

    Private Sub Workbook_Open()
    
        SHEETList = Array("TTL APAC", "Japan", "Korea", "HK Hub", "China", "HMT", "ANZ", "SEA")
    
        For Each sht In SHEETList
            Sheets(sht).Protect Password:="xxxxx"
        Next sht
    
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Protect/Unprotect Sheets in array with VBA

    Now it says "variable not defined". What do I need to defined it as?

    Dim SHEETList As Worksheet
    Does not seem to work...

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,333

    Re: Protect/Unprotect Sheets in array with VBA

    I never use Option Explicit so not used defining variables.

    Private Sub Workbook_Open()
        Dim SHEETList, sht
        SHEETList = Array("TTL APAC", "Japan", "Korea", "HK Hub", "China", "HMT", "ANZ", "SEA")
    
        For Each sht In SHEETList
            Sheets(sht).Protect Password:="xxxxx"
        Next sht
    
    End Sub

  6. #6
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Protect/Unprotect Sheets in array with VBA

    It's working now! had a tiny spelling error... Thanks!!

  7. #7
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Protect/Unprotect Sheets in array with VBA

    now it says: "Object doesn't support this property or method..."

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,333

    Re: Protect/Unprotect Sheets in array with VBA

    You're welcome and thanks for rep+.

+ 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. Made a VBA code to protect sheets... now I can't unprotect the sheets
    By ferocity02 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-25-2017, 08:17 PM
  2. VBA to unprotect sheets x 2, refresh data then protect sheets
    By kaseyleigh in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 04-18-2016, 08:38 AM
  3. [SOLVED] Protect/Unprotect sheets
    By Lukael in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-17-2014, 07:33 PM
  4. [SOLVED] Macro to Unprotect sheets-unlock range of cells-protect sheets
    By jrace in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2013, 10:45 AM
  5. [SOLVED] protect/unprotect all sheets
    By wayneg in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-03-2012, 08:20 AM
  6. [SOLVED] Password protect and unprotect all sheets EXCEPT two named sheets
    By Ducatisto in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-29-2012, 05:43 AM
  7. [SOLVED] protect/unprotect all sheets
    By roos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-13-2006, 06:50 PM

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