+ Reply to Thread
Results 1 to 8 of 8

Macro to create two pivots on the same page

  1. #1
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Macro to create two pivots on the same page

    Good afternoon and happy sunny Friday to all

    I am wondering if somebody could assist me! I have created a wonderful spreadsheet which uses VBA to collate tables, sum columns, append new sheets....and so on. I have one more task before the workbook is complete but I am totally stuck. I am trying to create pivot tables using a macro, however, as I am not very good at coding yet I simply recorded my actions for creating my pivots, and then pasted that code into my sub string. Unfortunately, even before the pivot can get going, I recieve an error saying " but keep getting an error saying "Run time error 5 - invalid procedure or arguement". I have edited the ranges but without success. Could this be because I have not declared any variables?

    Many thanks for any help.

    Cheers guys

    Please Login or Register  to view this content.
    Last edited by Ivor; 06-06-2011 at 07:02 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Macro to create two pivots on the same page

    Please post a sample workbook with some typical data and the code as above.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Re: Macro to create two pivots on the same page

    Quote Originally Posted by TMShucks View Post
    Please post a sample workbook with some typical data and the code as above.

    Regards
    Hello TM Shucks, I hope you had a good weekend. Thank you for your offer to help me sir, much appreciated. Please find attached a copy of the sheet containing the macro. In summary, the macro is supposed to create a new sheet and add two pivot tables, however the code falls over at the first stages (before the pivot can be created). As mentioned I have tried declaring variables, downsizing the criteria ranges but all without any success.


    Many thanks sir

    Ivor
    Attached Files Attached Files

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Macro to create two pivots on the same page

    See the attached updated workbook.

    Pivot Table coding is not my strong point so it's not ideal in that cells are selected ... but it seems to work and you can re-run it successfully.

    Assuming that it produces the results that you expect, it's now working ;-)

    Please Login or Register  to view this content.
    Regards
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Cool Re: Macro to create two pivots on the same page

    Quote Originally Posted by TMShucks View Post
    See the attached updated workbook.

    Pivot Table coding is not my strong point so it's not ideal in that cells are selected ... but it seems to work and you can re-run it successfully.

    Assuming that it produces the results that you expect, it's now working ;-)

    Please Login or Register  to view this content.
    Regards
    Hello Sir

    I don't know how you did it, but you did it!!! Thank you so very much. Pivots do appear to be an unknown entity when it comes to coding. Luckily for me the pivots will not change, so I can now go ahead and add in code to auto refresh the totals after each run. I am intrigued to know how you knew what to change to get the code to work? I will compare your code against my recorded one for the answer. I do need to add on one more pivot on a separate page, but I guess I can utilise the code you have given me to create that one also.

    Thanks again sir for your help, this is remarkable.


    Cheers
    Ivor

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Macro to create two pivots on the same page

    You're welcome. Thanks for the feedback and the rep.

    The critical part is this:

    Please Login or Register  to view this content.

    And then referring to newSheet rather than ActiveSheet wherever it was used. I'm sure there is a better way. When you add a sheet, it becomes the active sheet so, while you have hold of it, assign it to a variable that you can keep track of. Your code refers to Sheet1 which was fine when you first ran it but you get errors second time through.

    Anyway, it works, although it could probably be refined with some time and effort.

    Regards

  7. #7
    Registered User
    Join Date
    02-15-2010
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    76

    Smile Re: Macro to create two pivots on the same page

    Quote Originally Posted by TMShucks View Post
    You're welcome. Thanks for the feedback and the rep.

    The critical part is this:

    Please Login or Register  to view this content.

    And then referring to newSheet rather than ActiveSheet wherever it was used. I'm sure there is a better way. When you add a sheet, it becomes the active sheet so, while you have hold of it, assign it to a variable that you can keep track of. Your code refers to Sheet1 which was fine when you first ran it but you get errors second time through.

    Anyway, it works, although it could probably be refined with some time and effort.

    Regards
    Brilliant, and thanks again sir. There are some clever people in this company, they know Java, Agile and so on.... but no one here in the company knows about using VBA on pivots, so with you last bit of advice, I have something valuable against my knowledge (even though I stole it from you). Thanks again my man and no doubt I'll be on here asking for help again real soon, my spreadsheet is not yet complete....ha ha.

    Have a good day sir

    Cheers
    Ivor

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Macro to create two pivots on the same page

    even though I stole it from you
    Consider it a gift ;-)

+ 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