+ Reply to Thread
Results 1 to 5 of 5

Trying to create a pivot table with a macro

Hybrid View

  1. #1
    Registered User
    Join Date
    09-09-2015
    Location
    India
    MS-Off Ver
    2010
    Posts
    65

    Re: Trying to create a pivot table with a macro

    Hi Jenny,

    PFB for the required code and PFA for the sample data and macro file. I hope this code helps.



    Option Explicit
    
    Sub creating_pivot_tables()
    
    
    Application.ScreenUpdating = False
    
    Dim pivottable1 As PivotTable
    Dim pivotField1 As PivotField
    Dim last_row As Integer
    Dim range1 As Range
    
    'code for deleting pivot table in activesheet, if exist
    For Each pivottable1 In ActiveSheet.PivotTables
            ActiveSheet.Range(pivottable1.TableRange2.Address).Delete
    Next pivottable1
        
    last_row = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
    Set range1 = ActiveSheet.Range("A1:R" & last_row)
        
        
    
        
    Range("A1").Select
    Selection.AutoFilter
    
    ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range _
        ("M1:M" & last_row), SortOn:=xlSortOnValues, Order:=xlAscending         'sorting data in column M
        
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
        
        Selection.AutoFilter
            
        'applying first pivot on the data
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            range1, Version:=xlPivotTableVersion15).CreatePivotTable _
            TableDestination:="'[creating pivot tables.xlsm]Sheet3'!R2C21", TableName:= _
            "Pivot1", DefaultVersion:=xlPivotTableVersion15
        
    
        'Adding division column
        ActiveSheet.PivotTables("Pivot1").AddDataField ActiveSheet.PivotTables( _
            "Pivot1").PivotFields("DIVISION"), "Count of DIVISION", xlCount
            
        With ActiveSheet.PivotTables("Pivot1").PivotFields("DIVISION")
            .Orientation = xlRowField
            .Position = 1
        End With
        
        'selecting and copying existing pivot table
        ActiveSheet.PivotTables("Pivot1").PivotSelect "", xlDataAndLabel, True
        Selection.Copy
        
        'creating second pivot table
        ActiveSheet.Range("X2").PasteSpecial
        
        Application.CutCopyMode = False
        
            
        'renaming second pivot table
        ActiveSheet.PivotTables(1).Name = "Pivot2"
    
        'adding vendor name column
        With ActiveSheet.PivotTables("Pivot2").PivotFields("Vendor Name")
            .Orientation = xlRowField
            .Position = 2
        End With
        
        ActiveSheet.PivotTables("Pivot2").PivotFields("DIVISION").LayoutForm = _
            xlTabular
        
        ActiveSheet.PivotTables("Pivot2").PivotSelect "DIVISION[All;Total]", _
            xlDataAndLabel, True
            
        
        'removing subtotal from second pivot table
        With ActiveSheet.PivotTables("Pivot2")
            For Each pivotField1 In .PivotFields
                pivotField1.Subtotals(1) = False
            Next pivotField1
        End With
        
        Range("A1").Select
        
            
    End Sub
    Regards
    Ramandeep Singh
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Trying to create a pivot table with a macro

    Hello!
    Thank you very much for your reply! I will be back at work on Monday and will give it a try then. I'll be sure to come back and let you know how it goes.

    Jenny

+ 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. How to create a dynamic macro that will create pivot table
    By thlee1122 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-08-2015, 11:49 PM
  2. How to create a macro to create a pivot table on a new sheet
    By thlee1122 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2015, 11:14 AM
  3. Create a macro to create a pivot table in a worksheet
    By Triscia in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-15-2015, 10:53 AM
  4. Create 2 pivot table using macro
    By melforce in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2015, 09:19 PM
  5. Macro to create pivot table/pivot chart
    By Karina in forum Excel General
    Replies: 2
    Last Post: 10-27-2011, 10:33 PM
  6. [SOLVED] macro to create a pivot table
    By Catie79 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-22-2005, 06:10 PM
  7. macro to create pivot table
    By sandeeprs77 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2005, 12:57 PM

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