Results 1 to 4 of 4

Thread: [RESOLVED] VBA: Help needed to create pivot table

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    2

    Resolved [RESOLVED] VBA: Help needed to create pivot table

    Im an intern with a media company in Malaysia & I am asked to do automation work in Excel (I've no prior knowledge of VBA) . One of the tasks is to automate the creation of pivot tables from a set of data. I have some codes which I got from a book, & I've tried to experiment & play around with it to suit my task, but it still doesn't give me the desired result.

    Could anyone pls help me out? (no one in my company knows VBA ) I've included the excel sheet for the raw data & the pivot tables (2) done manually too.

    Below is the code that I've played with...(all 'should be ok' except the "Total (RM)" part)

    Code:
    Sub CreatePivotTable() 
        Dim PTCache As PivotCache 
        Dim PT As PivotTable 
        Dim LastRow As Long 
        Dim LastCol As Long 
        Dim rngSource As Range 
         
        Application.ScreenUpdating = False 
         
         'Delete PivotSheet if it exists
        On Error Resume Next 
        Application.DisplayAlerts = False 
         
        Sheets("PivotSheet").Delete 
        On Error Goto 0 
        Application.DisplayAlerts = True 
         
         'Create a Pivot Cache
        Set PTCache = ActiveWorkbook.PivotCaches.Add( _ 
        SourceType:=xlDatabase, _ 
        SourceData:=Range("A1").CurrentRegion.Address) 
         
         'Add new worksheet
        Worksheets.Add 
        ActiveSheet.Name = "PivotSheet" 
         
         'Create the pivot table from the cache
        Set PT = PTCache.CreatePivotTable( _ 
        TableDestination:=Sheets("PivotSheet").Range("A1"), _ 
        TableName:="ActChartPivot") 
         
        With PT 
             'Add fields
            .PivotFields("Product").Orientation = x1RowField 
            .PivotFields("CopyLine").Orientation = x1RowField 
            .PivotFields("Genre").Orientation = x1RowField 
            .PivotFields("Media").Orientation = x1RowField 
            .PivotFields("Duration").Orientation = x1RowField 
             
             'Add calculated items
            .PivotFields("RM").Orientation = x1ColumnField 
             '.PivotFields("Count of RM").CalculatedItems.Add _
             '"Q1", "=RM"
             
        End With 
         
    End Sub
    All help much appreciated!

    - fwawergurl16
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA: Help needed to create pivot table

    try to record a macro of creating the pivot table manually, you can then modify the code to suit your application

    if you have problems post the code here with details of the problem
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    2

    Re: VBA: Help needed to create pivot table

    Thank you for helping, westconn1 I had my problem solved now. Thank u once again!!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] VBA: Help needed to create pivot table

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width