+ Reply to Thread
Results 1 to 2 of 2

Hot to Create a Pivot Table with VBA

  1. #1
    Registered User
    Join Date
    11-21-2014
    Location
    Dalton, GA
    MS-Off Ver
    2010
    Posts
    13

    Hot to Create a Pivot Table with VBA

    Hello All,

    I am new to using VBA to create Pivot tables and need it to automate a report where I take Data from several worksheets and create pivot tables on the first worksheet for every other worksheet in the workbook (but the worksheets will be deleted at the end of every week and repopulated with new worksheets in the same order with similar, but not the same, names). The format will be close to the same but the column headings may have to be adjusted so I think I will have to create each macro individually as opposed to creating a loop. I think I can do all the adjustments once I have a working Macro for the first one. I think I have compiled a mixture from other examples to do what I want it to do but I keep running into issues when it comes to actually creating the pivot table. I really can't seem to find any simple way to understand the format of VBA as it relates to Pivot Tables. Can anyone take a look at this code and tell me what syntax errors I seem to be making?


    The error is saying "Variable not defined" when I get to the last section of this code.


    __________________________________________________________________________________________________________________

    Sub CreatingPivotTables()
    '
    ' CreatingPivotTables Macro
    '

    '

    Dim WSD As Worksheet
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    Dim PRange As Range
    Dim FinalRow As Long
    Dim FinalCol As Long
    Dim PivotDest As Worksheet
    Set WSD = Worksheets(3)
    Set PivotDest = Worksheets("1Main ")

    ' Delete any prior pivot tables
    For Each PT In WSD.PivotTables
    PT.TableRange2.Clear
    Next PT

    ' Define input area and set up a Pivot Cache
    Worksheets(3).Activate
    FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
    FinalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column
    Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
    SourceData:=PRange)



    Set PT = PTCache.CreatePivotTable(TableDestination:=PivotDest.Cells(30, 2), _
    TableName:="PivotTable100")


    PT.ManualUpdate = True


    ' Set up the row & column fields (This is where I am having the issues according to VBA errors. It's saying Variable Not Defined when it gets to "x1RowField")
    With PT
    .PivotFields("UPC #").Orientation = x1RowField
    .PivotFields("Dollar on Hand").Orientation = x1DataField
    .PivotFields("Quantity on Hand").Orientation = x1DataField
    .PivotFields("Dollar Sold").Orientation = x1DataField
    .DisplayFieldCaptions = False
    End With


    End Sub

    _______________________________________________________________________________________________________________________________________________


    Please Help!
    Last edited by ChristopherBrandonKi; 11-25-2014 at 10:36 AM. Reason: Clarification

  2. #2
    Forum Contributor
    Join Date
    09-05-2012
    Location
    Dubai
    MS-Off Ver
    Office 365
    Posts
    409

    Re: Hot to Create a Pivot Table with VBA

    Hi.
    would be better if you post data... show you desired result of pivot table..... Do it manually first and attach

+ 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. Macro to create pivot table/pivot chart
    By Karina in forum Excel General
    Replies: 2
    Last Post: 10-27-2011, 10:33 PM
  2. Replies: 0
    Last Post: 09-01-2011, 12:07 PM
  3. Create multiple pivot charts from one pivot table
    By SueWithQuestion in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 07-13-2011, 12:29 PM
  4. [SOLVED] Create Pivot Table: Cannot Open Pivot Table Source File
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-05-2010, 05:11 AM
  5. Create pivot table from exisiting pivot table
    By cmb80 in forum Excel General
    Replies: 1
    Last Post: 09-18-2009, 07:10 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