+ Reply to Thread
Results 1 to 2 of 2

Error when unprotecting sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    09-03-2012
    Location
    Christchurch New Zealand
    MS-Off Ver
    Excel 2013
    Posts
    45

    Error when unprotecting sheet

    Hoping someone can help
    Im not sure if its because of the array the sheets are in
    Any help appreciated

    Private Sub CommandButton1_Click()
    
    Application.EnableEvents = False
    Dim iRet As Integer
        Dim strPrompt As String
        Dim strTitle As String
     
        ' Promt
        strPrompt = "Have you saved any Changes?"
     
        ' Dialog's Title
        strTitle = "Warning Changes won't be Saved"
     
        'Display MessageBox
        iRet = MsgBox(strPrompt, vbYesNo, strTitle)
     
        ' Check pressed button
        If iRet = vbNo Then
        ThisWorkbook.Save
    
        Else
            If iRet = vbYes Then
            Dim MyArray As Variant
            Dim i As Long
        MyArray = Array("December", "Totals")
        Sheets(MyArray).Select
        For i = LBound(MyArray) To UBound(MyArray)
            With Worksheets(MyArray(i))
                .Unprotect
    End With
    Next i
        ActiveWindow.FreezePanes = False
        ActiveWindow.View = xlPageLayoutView
            
         End If
         
    
    Dim DataWorkbook As Workbook
    Dim SaveFileName As String
    
    Set DataWorkbook = ActiveWorkbook
    DataWorkbook.Sheets(Array("December", "Totals")).Copy
    With ActiveSheet
        .UsedRange = .UsedRange.Value
    End With
        
    SaveFileName = "\\Generated Reports\" & Format(Now, "yyyymmdd") & "Report.xlsx"
    ActiveWorkbook.SaveAs Filename:=SaveFileName
    MsgBox "New Report Created and Saved As" & SaveFileName
    ActiveWorkbook.Close
    ThisWorkbook.Close False
     End If
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Error when unprotecting sheet

    Hi,

    if there's a password to unprotect the sheets, then add that. Another for looping through the specific sheets and unprotecting them is:
    Dim ws As Worksheet
    For Each ws In Sheets(Array("December", "Totals"))
        ws.Unprotect 'and password if needed
    Next ws
    Best,
    berlan

+ 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. Unprotecting sheet
    By familylink in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2011, 02:35 AM
  2. Error on unprotecting worksheet
    By rkayasth in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2011, 09:31 PM
  3. Macro protecting & Unprotecting sheet showing error.
    By coolhit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2009, 04:02 PM
  4. Protecting/unprotecting runtime error
    By zerodegreec in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2009, 02:06 AM
  5. Error unprotecting worksheet
    By Chad in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-04-2005, 01:06 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