+ Reply to Thread
Results 1 to 4 of 4

VBA Copy Worksheets, Changing Pivot Cache, Run Time Error 5

  1. #1
    Registered User
    Join Date
    03-23-2022
    Location
    Germany
    MS-Off Ver
    2019
    Posts
    2

    VBA Copy Worksheets, Changing Pivot Cache, Run Time Error 5

    Hi helping folks and gods of coding.

    As a newbee in vba codes without any deeper knowledge I check forums like this to find answers to what I have done wrong. But for this problem I have not found a fittig solution or at least any I could crasp. So I have to ask dirctly and hope for helpfull answers.

    ATM I'm working on a vba code to copy multiple sheets containing pivot charts/tables with and without data slicers as well as their data source to a new workbook.

    All pivot share the same data source organised as a table (Listobject). That code worked fine until I discovered that the pivots still refered to the original workbook.

    I searched the internet and found at TheSpreadsheetGuru.com a solution to change the PivotCache via vba. That code on itself worked fine too, if run directly in the new workbook. So i tried to implement the changing of the PivotCache into the code to copy the sheets to the new worksheet. The result was folowing code:

    Private Sub cmdTest_Click()
    ' Copy worksheets and change PivotCache
    'SOURCE: TheSpreadsheetGuru/The-Code-Vault (part of code for PivotCache change)

    ' Variables for loop

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim pt As PivotTable

    ' Variables for changing PivotCache

    Dim Data_sht As Worksheet
    Dim Pivot_sht As Worksheet
    Dim PivotName As String
    Dim NewRange As String
    Dim Source As String

    ' Variables for filename and path

    Dim Path As String
    Dim NewName As String

    Path = ActiveWorkbook.Path
    NewName = "HH_Plan " & Worksheets("Steuerungselemente").Range("D5").Value & "_" & _
    Worksheets("Steuerungselemente").Range("D7").Value & " Diagramme.xlsx"
    'MsgBox ("name: " & Path & NewName)

    ' Copy Sheets to new Workbook

    Worksheets(Array("Gesamthaushalt", "Teilhaushalt", "Planansätze", "Stammdaten")).Copy

    ' Set Variables for PivotCache

    Set Data_sht = ThisWorkbook.Worksheets("Planansätze") 'Sheet with data source
    Source = "tbl_planansatz" ' Data Source (= Table / ListObject)
    NewRange = Data_sht.Name & "!" & Source ' New range for PivotCache

    ' Loop for PivotCache change and value format

    Set wb = ActiveWorkbook
    For Each ws In wb.Sheets
    For Each pt In ws.PivotTables
    With pt
    .ChangePivotCache ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
    .PivotCache.Refresh
    For Each pf In .DataFields
    pf.NumberFormat = "#,##0 €"
    Next pf
    .ColumnRange.HorizontalAlignment = xlCenter
    .PivotCache.Refresh
    End With
    Next pt
    Next ws

    ' Loop for sheet protection (inactive for testing)

    'For Each ws In wb.Sheets
    ' With ws
    ' .Protect Password:="xxx", DrawingObjects:=False, Contents:=True, Scenarios:= _
    ' False, AllowFiltering:=True, AllowUsingPivotTables:=True
    ' End With
    'Next ws


    ' Hide Source Data sheets and workbook protection. Save to Filename and path

    Worksheets(Array("Planansätze", "Stammdaten")).Visible = False
    Worksheets("Gesamthaushalt").Activate
    ActiveWorkbook.ShowPivotTableFieldList = False
    'ActiveWorkbook.Protect Password:="xxx", Structure:=True, Windows:=False
    ActiveWorkbook.SaveAs Filename:=Path & "\" & NewName
    End Sub

    Running the code results in a Runtime Error 5: Invalid Procedure Call or Argument statement when hitting the line

    .ChangePivotCache ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)

    As running the code for copying without changing the PivotCache and running the PivotCache change after copying the sheets doesn't result in an error, the run-time error must be connected to the combination of both steps. I guess the changing of the PivotCache can't be done in this state of the new workbook, but I cant think of any workaround to a problem I don't understand. So I musst surrender to excel and vba.

    As perhaps the one or the other might guess from the names of the sheets I normaly communicate in german . So please excuse my partly broken and clumsy attemps or outright assaults on english language and gramma. But I hope that you can help me nontheless.

    Thanks in advance and kind regards

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,359

    Re: VBA Copy Worksheets, Changing Pivot Cache, Run Time Error 5

    I think this:
    Please Login or Register  to view this content.
    Needs to be:
    Please Login or Register  to view this content.
    ThisWorkbook is always the workbook where the code resides.
    ActiveWorkbook is the active workbook regardless of where the code resides.
    wb in this instance has been defined as ActiveWorkbook so we should use this.
    Last edited by CheeseSandwich; 03-23-2022 at 07:31 AM.

  3. #3
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,359

    Re: VBA Copy Worksheets, Changing Pivot Cache, Run Time Error 5

    You may also need to change:
    Please Login or Register  to view this content.
    To:
    Please Login or Register  to view this content.
    Hope this helps

  4. #4
    Registered User
    Join Date
    03-23-2022
    Location
    Germany
    MS-Off Ver
    2019
    Posts
    2

    Re: VBA Copy Worksheets, Changing Pivot Cache, Run Time Error 5

    Quote Originally Posted by CheeseSandwich View Post
    I think this:
    Please Login or Register  to view this content.
    Needs to be:
    Please Login or Register  to view this content.
    ThisWorkbook is always the workbook where the code resides.
    ActiveWorkbook is the active workbook regardless of where the code resides.
    wb in this instance has been defined as ActiveWorkbook so we should use this.
    Thanks

    Please Login or Register  to view this content.
    helped.

    I allready thoght that it might be something "small" and "very basic".

    The Structure of a code is something I might crasp and I can often tinker my way to a working code. But I really lack gramma. Time for taking some VBA training lessions :-)

    Again, thanks for the awesome and fast help

    Kind Regards

+ 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. Pivot cache error debug
    By dorabajji in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-06-2019, 11:38 AM
  2. VBA Pivot Table Cache Error
    By Fossette in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-11-2018, 06:36 AM
  3. [SOLVED] Macro to update pivot tables and use the SAME cache instead of repeatedly recreating cache
    By Fowzee1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2015, 11:26 PM
  4. HOW TO -Using same Pivot cache -different worksheets
    By pdalal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-07-2014, 03:15 PM
  5. Pivot cache error
    By Ricardo Mass in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2013, 05:06 AM
  6. Changing Range on Pivot Cache
    By Wyvern in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-28-2006, 10:16 PM
  7. error message with Pivot Cache
    By Wolfram Schneider in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-26-2005, 02:06 AM

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