+ Reply to Thread
Results 1 to 11 of 11

VBA Build Pivot Table Help

  1. #1
    Registered User
    Join Date
    01-18-2016
    Location
    Arizona
    MS-Off Ver
    2016
    Posts
    36

    VBA Build Pivot Table Help

    Good Evening,

    A few months ago I started learning VBA and have had a great time learning. Today I need some assistance on my pivot table building.

    My macro works fine when I use it, Excel 2013, but when an associate did a quick test it errored out on them, he uses Excel 2007. The associate was in a rush but I was able to do a quick debug and it yellowed the section in my code that starts building the pivot table (highlighted in red). My macro does a lot of pre work to the data before building the pivot table so the below code is about 1/3 down in my macro code:


    'Create Tab For Pivot View'
    Sheets.Add After:=ActiveSheet
    Sheets("Sheet2").Select
    Sheets("Sheet2").name = "Pivot"


    'Build Pivot Table'
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Data!R1C1:R1048576C14", Version:=xlPivotTableVersion15).CreatePivotTable _
    TableDestination:="Pivot!R5C5", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion15


    Sheets("Pivot").Select
    Cells(5, 5).Select
    ActiveSheet.PivotTables("PivotTable1").RowAxisLayout xlTabularRow
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Unit Manager")
    .Orientation = xlRowField
    .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Associate Manager")
    .Orientation = xlRowField
    .Position = 2
    End With

    I used the macro recorder to record the above code. Am I having issues with other people running my macro because I don't have all that other code I see in peoples macros like:

    Dim sht As Worksheet
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String

    Is all the above code necessary? If it helps my data set is column A - M, and 200 rows. Thx for any help you can provide.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: VBA Build Pivot Table Help

    Hi mrazexcel,

    Look at the maximum number of rows for Excel 2007 vs your 2016.
    I think 2007 only allows 1 million rows and your code tries to address more than that...

    I'd suggest you study up on Dynamic Named Ranges and use the Name Range for your pivot array. Then it might work on both machines.
    http://www.ozgrid.com/Excel/DynamicRanges.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    01-18-2016
    Location
    Arizona
    MS-Off Ver
    2016
    Posts
    36

    Re: VBA Build Pivot Table Help

    Marvin thats a great call out on the max row difference between excels. When i get back to work in the morning I will give that a shot. Thx

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA Build Pivot Table Help

    get rid of this
    Please Login or Register  to view this content.
    also read this guide to help you along with VBA pivot tables
    http://www.thespreadsheetguru.com/bl...l-pivot-tables
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  5. #5
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA Build Pivot Table Help

    Oh, get rid of this one too
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-18-2016
    Location
    Arizona
    MS-Off Ver
    2016
    Posts
    36

    Re: VBA Build Pivot Table Help

    Thx humdingaling, I've googled why you recommend getting rid of them and see the different versions of excel 12, 14, 15. So you believe the below code will be fully functional:

    'Build Pivot Table'
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Data!R1C1:R1048576C14").CreatePivotTable _
    TableDestination:="Pivot!R5C5", TableName:="PivotTable1"

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: VBA Build Pivot Table Help

    I can't wait to see if his solution works. I'm still guessing the R1048576 is too big.

    Please let us know when you test it.

  8. #8
    Registered User
    Join Date
    01-18-2016
    Location
    Arizona
    MS-Off Ver
    2016
    Posts
    36

    Re: VBA Build Pivot Table Help

    Roger that, will reply tomorrow on my findings.

  9. #9
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA Build Pivot Table Help

    Excel 2007 Worksheet size
    1,048,576 rows by 16,384 columns

    should be fine
    if that is an issue just dial back the number
    i'm pretty sure your not actually using 1048576 rows of data

  10. #10
    Registered User
    Join Date
    01-18-2016
    Location
    Arizona
    MS-Off Ver
    2016
    Posts
    36

    Re: VBA Build Pivot Table Help

    Hi, an update:
    First I tried adjusting the range to a smaller size. Macro still threw an error. Second I tried removing the verison15 in the code and bingo that did the trick. Thx to both of you for your input.

  11. #11
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA Build Pivot Table Help

    not a problem

    just for learning...you really shouldnt allocate your data source to be more than you require
    if for whatever reason you cannot be exact....a reasonable guesstimate of maximum number of rows should be used
    ie 10k or 100k...if you are actually requiring 1 million+ rows...then you should probably be looking at a database

    the link i posted on #4 i use quite often to refresh my memory on syntax for pivot tables
    its well worth a study/read

+ 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 build a Loan table with a Pivot Table
    By Julio Lescano in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 02-18-2014, 11:20 AM
  2. Replies: 4
    Last Post: 11-18-2013, 11:59 AM
  3. [SOLVED] Build Table using selected fields from Table of data
    By amartin575 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2013, 01:45 PM
  4. Use visible rows from autofilter to build Pivot table. Or use visible rows to Copy/Paste
    By mwhitedesigns in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2012, 10:34 AM
  5. Macro to Build a Pivot Table
    By Staci in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-26-2011, 04:40 PM
  6. Macro to build pivot table based on another pivot table
    By tcowen61 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-31-2009, 10:41 AM
  7. pivot table build using excel macro
    By jnewl in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-16-2006, 08:55 AM
  8. How can I build a pivot table from multiple worksheets which are .
    By Richard Stephens in forum Excel General
    Replies: 2
    Last Post: 02-26-2005, 07:06 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