+ Reply to Thread
Results 1 to 3 of 3

Automate a pivot table

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2008
    Posts
    33

    Automate a pivot table

    Hi all,
    I have a workbook that starts blank everytime time its opened, and at the push of a button launches a macro that imports some data, sorts and formats it. All good so far. I am able to create a pivot table (using the wizard) to analyse the data I am left with and display the desired results, but I would like to be able to automate this process, so that the table 'appears' when the original macro has done its work and sorted the data. I have tried (cheating?) recording a macro of the pivot table wizard, but it fails to run and I am not capable of fixing the programing.
    Is there an easy way to automate the pivot table process or a macro that can be written that will work?
    Here is the code generated by the record macro function
     ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
            "Sheet1!C6:C8").CreatePivotTable TableDestination:= _
            "'[CRM data1]Sheet1'!R31C9", TableName:="PivotTable6", DefaultVersion:= _
            xlPivotTableVersion10
        ActiveWorkbook.ShowPivotTableFieldList = True
        With ActiveSheet.PivotTables("PivotTable6").PivotFields("Package")
            .Orientation = xlPageField
            .Position = 1
        End With
        With ActiveSheet.PivotTables("PivotTable6").PivotFields("Location")
            .Orientation = xlRowField
            .Position = 1
        End With
        ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _
            "PivotTable6").PivotFields("Stand_Size"), "Count of Stand_Size", xlCount
        ActiveWorkbook.ShowPivotTableFieldList = False
        Range("I31").Select
        ActiveSheet.PivotTables("PivotTable6").PivotFields("Count of Stand_Size"). _
            Function = xlSum
    End Sub
    It hangs up at the first 'with' statement, not sure why, but its a simple table, with only the 3 columns of data in it. Any help will be much appreciated,
    Cheers,


    EDIT: It pays to have a thorough check of all code eh? For whatever reason, when the macro was recorded, it give erroneous array for the source data - once I spotted this and changed it to the correct values, it all works.
    Last edited by BLRITCHIE; 11-18-2009 at 01:48 AM.

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Automate a pivot table

    Hi,

    It looks as if the source table, in the first instruction, is not correct ...only column C and two rows ...

    HTH

  3. #3
    Registered User
    Join Date
    04-20-2008
    Posts
    33

    Re: Automate a pivot table

    Hi, thanks for your reply - I solved it myself and then edited my post without seeing your answer (had the page open in my browser and didnt refresh - you were spot on though!).
    Cheers, appreciate your time.

+ 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