Results 1 to 34 of 34

VBA Producing Error Code 1004

Threaded View

  1. #1
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    VBA Producing Error Code 1004

    Can anyone help with this VBA issue .....

    I have 7 sheets in a workbook (1 data table and 6 worksheets housing pivot tables/charts (PivotTable1) all running off the data table in sheet 1).

    I am trying to write this code to enable pivot table refresh when data is inputted into the data table followed by a protection of the sheet(s). The code will unprotect the sheet prior to the refresh before protection after the refresh.

    Code below:

    Module1:

    Sub Refresh(ByVal sh As Object)
        Dim pvt As PivotTable
        Application.ScreenUpdating = False
        If sh.PivotTables.Count > 0 Then
            sh.Unprotect Password:="1"
            For Each pvt In sh.PivotTables
                pvt.RefreshTable
                pvt.Update
            Next pvt
        End If
        sh.Protect Password:="1", AllowUsingPivotTables:=True
        Application.ScreenUpdating = True
    End Sub
    ThisWorkbook:

    Private Sub Workbook_SheetActivate(ByVal sh As Object)
    Call Refresh(sh)
    End Sub
    (nothing within the individual sheet code boxes)

    When I input data into the data table then click on the immediate sheet following the data table (sheet 2), the pivot refreshes fine and protects. However, as I click on the other sheets I get the following error code:

    Run-time error '1004'

    That command cannot be performed while a protected sheet contains another pivot table report based on the same source data.

    To remove protection from the sheet that has the other report, click the sheet tab, and then click unprotect sheet (review tab, changes group). Then try the command again.

    I am able to click on 'End', 'Debug' and 'Help'.

    Clicking on End allows the sheet refresh, however I still get the error on the pages when clicking on them.

    The debug highlights 'pvt.RefreshTable' in the refresh code in Module1.

    It seems the error is a direct result of the sheets already being protected somehow (a beginners assumption).

    Can anyone help with the problem?

    Thanks

    Luke
    Last edited by luke.walker; 06-28-2014 at 04:08 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Develop Organization Structure
    By kcasey1318 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-16-2013, 06:38 PM
  2. Trying to develop a bell curve model
    By JoshD75 in forum Excel General
    Replies: 2
    Last Post: 04-25-2009, 01:44 AM
  3. further develop IF(A2=A1+1;B1+1;1)
    By medicine stud in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-29-2007, 06:51 PM
  4. [SOLVED] How do I develop a forum in Excel?
    By cloud in forum Excel General
    Replies: 0
    Last Post: 03-10-2006, 02:45 AM
  5. Need help for develop Excel 2003 Add-ins
    By JohnLi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-19-2005, 04:06 AM

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