+ Reply to Thread
Results 1 to 4 of 4

LastRow on Changing Pivottable

  1. #1
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    LastRow on Changing Pivottable

    Hi guys

    I've been lurking on here for a while and haven't really posted in the VBA section up to now as I've been able tofind the solutions that I want simply by searching.

    However, I haven't had any luck with this query and am hoping somebody can help out.
    Am using Excel 2007, only using VBA for the past 3weeks, and am an accountant not a programmer. However, i can say that I'm very good at using excel spreadsheets and know how to use sumproduct, array formulas etc.

    I have a pivottable, and am using VBA to copy pivottable info across to an added sheet.
    The pivottable will have 3 separate states (each state has a different number of fields that are displayed) that will need to be copied across to the one single sheet.
    I'm writing the macro to copy from Pivot sheet, paste into target, then switch back to pivot, adjsut pivot, copy, then paste into target and so on..

    Pivot1 is the Pivottable
    Julia_Inhouse is the Targetsheet

    I've managed to copy across the first state of the pivot table by using this code:
    You can see that I've used

    Please Login or Register  to view this content.
    So far so good. The info pastes into the target sheet.
    Now I need to go back to the pivottable and add another field.
    This is the Pivottable in the second state, whcih has more info than the first.
    One additional column and more rows.
    So i've changed the column ref to "F", but I've left the LastRow4 reference the same.
    However, when I run the macro, the LastRow4 reference still refers to the lastrow when the Pivottable was in its first state.


    Please Login or Register  to view this content.

    My question is how to update the LastRow formula so that I can capture all info from A3 to lastrow (including grand totals) when pivottable is in its second state.

    Will the solution to this will suffice for when the pivottable is in its 3rd state i.e. after another field is added, and i need to copy the whole pivottable contents to sheet Julia_inhouse.

    Alternatively, can you suggest another way of selecting the pivottable data from A3 to the last row.

    Note that the xlLast cell function cannot be used.
    This is because the pivottable is used first before the macro is to be run.
    When the pivottable is used prior to any macros, it displays all fields of data which results in the excel last cell being 1,000s of lines down. Then when the macro is being run, the xlLastCell will be on a row far below the last line of each of the 3 states of the pivottable that I want to copy to new sheet.


    Thanks.
    Last edited by rasonline; 03-07-2011 at 11:28 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: LastRow on Changing Pivottable

    The pivot table object has various range references you can use to determine its location/coveage.

    Please Login or Register  to view this content.
    Object browser and help will provide more info.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: LastRow on Changing Pivottable

    Your info has prompted me to do further internet searching, and i"ve found this very useful link which otehr users might also want to look at if they are reading this thread.

    Am still trying to work it out. Will return back to re-mark thread title once solved.

  4. #4
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: LastRow on Changing Pivottable

    Thank you sir!
    Works like a charm.

    After 10 years of working on Excel and being the "fundi" amongst fellow accountants, I can honestly say that I've learnt that a whole new world exists with VBA.. loving it!!! Hell, it's addictive.

+ 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