+ Reply to Thread
Results 1 to 2 of 2

Using Macro recorder with Pivot Tables

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2012
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Using Macro recorder with Pivot Tables

    Hi guys,

    I am trying to build a macro for the creation of pivot tables. Since I am very new to excel, i cannot code this myself and have resorted to using the macro recorder.

    However, after i have recorded my macro and i try to run it i get the following error:

    Run-time error '5'

    Invalid procedure call or argument


    In the debugger, this portion of the code is highlighted(it is right at the top):

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Graydon-TL!R1C1:R15507C17", Version:=xlPivotTableVersion14). _
            CreatePivotTable TableDestination:="Sheet3!R1C1", TableName:="PivotTable4" _
            , DefaultVersion:=xlPivotTableVersion14


    Would anyone please be able to explain how I should go about fixing this?

    What I am doing doesnt even seem to complex. I simply start the recorder, hit insert pivot chart, set up the correct fields in the pivot table, fix up the pivot chart type and then I stop recording.


    Any help, tips or pointers would be greatly appreciated!


    Cheers,
    Nima
    Last edited by arlu1201; 09-05-2012 at 04:06 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Using Macro recorder with Pivot Tables

    Hi, Nima,

    if you recorded the macro and run it without deleting the Pivot Table that has been used for recording the macro will try to build a new PT under the very same name:

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Graydon-TL!R1C1:R15507C17", Version:=xlPivotTableVersion14). _
            CreatePivotTable TableDestination:="Sheet3!R1C1", TableName:="PivotTable4" _
            , DefaultVersion:=xlPivotTableVersion14
    As the range fopr the data may differ please try this piece of code instead where both Sheetname as well as Name of PT are passed to the code:

    Sub Call_Build_PT()
    
      PT_Build "Sheet4", "PT_Nima_4"
    
    End Sub
    Sub PT_Build(strTab_PT As String, strPT_Name As String)
        
      ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
          SourceData:=Sheets("Graydon-TL").Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), _
          Version:=xlPivotTableVersion14).CreatePivotTable _
          TableDestination:="'" & strTab_PT & "'!R3C1", _
          TableName:=strPT_Name, _
          DefaultVersion:=xlPivotTableVersion14
    
    End Sub
    There must be a Sheet4 in your workbook and no PT called PT_Nima_4. If you want to run the code again, either delete the PT or pass a new name for it.

    Ciao,
    Holger

+ 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