+ Reply to Thread
Results 1 to 4 of 4

Workbook protection macro only works on step thru

Hybrid View

  1. #1
    Registered User
    Join Date
    12-15-2009
    Location
    Houston, TX, USA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Question Workbook protection macro only works on step thru

    Hi,
    I have a workbook that is connected with a SharePoint list. When the workbook is refreshed, the list populates a table in the workbook. The workbook should remain protected, allowing only sort, autofilter, pivot tables, and cell formatting.

    Right now I have code to remove formatting and autofilters when the workbook is opened, then protect it. A second macro is run via a button, and it is supposed to unprotect the work book, copy and fill some calculations, then protect it again.

    Here are the two bits:
    Private Sub Workbook_Open()
    
    Sheets("Input").Activate
    ActiveSheet.Unprotect
    ActiveSheet.AutoFilterMode = False
    Range("Table_owssvr_1").ClearFormats
    ActiveSheet.ListObjects("Table_owssvr_1").TableStyle = "TableStyleMedium9"
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    
    End Sub
    Sub Refresh_Data_and_Pivots()
    '
    Sheets("Input").Activate
    ActiveSheet.Unprotect
    ActiveWorkbook.RefreshAll
    Range("AY1:CH1").Copy
    ActiveSheet.Paste Destination:=Range("AY4")
    Range("AY4:CH4").AutoFill Destination:=Range("AY4:CH" & Cells(Rows.Count, "A").End(xlUp).Row), Type:=xlFillDefault
    Sheets("Customer_Segment").PivotTables("PivotTable1").RefreshTable
    Range("A3").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    End Sub
    The problem is that on the second macro, the ActiveWorkbook.RefreshAll command throws the following message:

    "The cell or chart that you are trying to change is protected and therefore read-only. To modify a protected cell or chart, first remove the protection Using the Unprotect Sheet command."

    The funny thing is, I can step through using F8, and it works just fine.
    Last edited by smokebreak; 12-01-2010 at 04:16 PM. Reason: added additional details

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,173

    Re: Workbook protection macro only works on step thru

    There may be experts around who can diagnose the problem from the code alone but, for me, the workbook is an essential input to the diagnostic process.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-15-2009
    Location
    Houston, TX, USA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Workbook protection macro only works on step thru

    I really have no way of anonymizing this data sorry

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,173

    Re: Workbook protection macro only works on step thru

    Good luck.

+ 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