+ Reply to Thread
Results 1 to 4 of 4

Get an error message when I try to unprotect a sheet using VBA. Why?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-30-2011
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    107

    Get an error message when I try to unprotect a sheet using VBA. Why?

    I have protected a sheet using:

    Sub ProtectSheet()
        
        Sheet1.Protect AllowFormattingCells:=True, AllowSorting:=True, AllowFiltering:=True, UserInterfaceOnly:=True
        Sheet1.EnableOutlining = True
    
    End Sub
    When I click a button that runs the following macro, it works:

    Sub UnprotectSheet()
        Sheet1.Unprotect
    End Sub
    When I make a worksheet change which triggers the following sequence:

    Private Sub Worksheet_Change(ByVal target As Excel.Range)
        'Application.EnableEvents = False
        Call PerfromAction(target)
        'Application.EnableEvents = True
    End Sub
    
    
    Sub PerfromAction(t As Range)
    
        If dbg = False Then On Error GoTo ErrHandler
    
        'Protection start
        Dim bProtected As Boolean
        bProtected = CheckProtection
        Call UnprotectSheet
    
    ... more
    
    End sub
    
    Sub UnprotectSheet()
        Sheet1.Unprotect
    End Sub
    Then I get the attached error message on the line Sheet1.Unprotect. I tried adding Application.DisplayAlerts = False before unprotecting. But then the macro stopped already on Application.DisplayAlerts = False and the popup appeared. If I tell the macro to continue, it continues and finishes successfully despite the popup.

    Why does this popup appear?
    Run time error.png

    PS. I can't upload the workbook as is because it contains lot's of company data. If I remove everything from the workbook, I don't get an error message any more when running the exact same macro. I don't know why.
    Last edited by OldManExcellor; 11-04-2014 at 07:55 AM.

  2. #2
    Registered User
    Join Date
    11-15-2012
    Location
    Göteborg, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Get an error message when I try to unprotect a sheet using VBA. Why?

    Do you have a global variable named sheet1 that refers to Worksheets(1)?

  3. #3
    Forum Contributor
    Join Date
    05-30-2011
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    107

    Re: Get an error message when I try to unprotect a sheet using VBA. Why?

    No, sheet1 is the codename of the protected sheet.

  4. #4
    Forum Contributor
    Join Date
    05-30-2011
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    107

    Re: Get an error message when I try to unprotect a sheet using VBA. Why?

    I have found that if execute

    Application.GenerateTableRefs = xlGenerateTableRefA1

    before I run the unprotect makro, it works.

    WHY is it like this? I am pulling my hair.

+ 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. vba changing specific sheet to protect/unprotect but there is an error
    By mcmunoz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-01-2013, 12:02 AM
  2. [SOLVED] Error message:Cannot rename a sheet to the same name as another sheet
    By AB33 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-12-2012, 11:14 AM
  3. Macro to unprotect sheet - run macros - then unprotect sheet is not working
    By AnnieBrownTX in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2012, 06:59 PM
  4. VBA For Error Message If Value is Referenced in Sheet
    By BrownTeddyBear in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2009, 03:20 AM
  5. [SOLVED] Unprotect Sheet - Removal of standard Message
    By Darin Kramer in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-31-2005, 10: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