+ Reply to Thread
Results 1 to 12 of 12

Macro that creates new column and creates pivot with filter based on new column

  1. #1
    Registered User
    Join Date
    01-19-2012
    Location
    US, United States
    MS-Off Ver
    Excel 2010
    Posts
    10

    Macro that creates new column and creates pivot with filter based on new column

    Problem: I have an extract from our accounting system that I want to do three things with:

    1) Insert a column titled "Project Number" and have that column look at the right-most four characters of the column that precedes it (one column to the left) - simple task, except for the number of rows is dynamic (weekly extract), and I need help with the function that looks at the preceding column to determine the number of rows to autofill.

    2) Create a pivot based on the table - simple task

    3) Create a filter based on four "Project Number" fields that may or may not exist in the table.

    Simply stated, I want to create a new column, titled "Project Number", pivot the table, and filter the pivot based on "Project Number" even if the project number does not exist in the current table. I am trying to report spending based on four projects that our COO is paying very close attention to.

    Expected outputs would be a pivot that filters based on the four project numbers and displays it in an orderly (sorted) fashion.

    I can get as far as filtering on the project number, but macro errors out if the project I am looking for is not in the data table to begin with.

    Let me know if you need clarification,

    BW

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro that creates new column and creates pivot with filter based on new column

    It will be good if you attach a sample file containing some dummy data (the file should match your original file with respect to layout, type of data, etc) so we can assist you faster.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    01-19-2012
    Location
    US, United States
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro that creates new column and creates pivot with filter based on new column

    Please see attachedTestFile - Copy.xlsx

    The Green column on sheet 1 is the column I would add, then pivot on all the data, with project number the key. I would like to filter automatically the project numbers listed, even if they are not present in the 'parent' data. That is, the file changes weekly, and sometimes all four projects are not present. When this is the case, the current macro I have (a simple recorded macro) errors out. Also, I would like the formatting to be 'comma.'

    Thank you for your help!

    BW

  4. #4
    Registered User
    Join Date
    01-19-2012
    Location
    US, United States
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro that creates new column and creates pivot with filter based on new column

    Is the attached file (prior post) suffiicient for the demonstration of what I wish to accomplish?

    Thank you,

    BW
    Last edited by BW78; 02-13-2012 at 05:51 PM.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro that creates new column and creates pivot with filter based on new column

    Its sufficient. Will have a macro ready for you shortly.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro that creates new column and creates pivot with filter based on new column

    Had some internet problems yesterday and couldnt post this.
    Try this code -
    Please Login or Register  to view this content.

  7. #7
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro that creates new column and creates pivot with filter based on new column

    I would make a few adjustments to that
    Please Login or Register  to view this content.
    Good luck.

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro that creates new column and creates pivot with filter based on new column

    Just for my learning - whats the difference in both the codes?

  9. #9
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro that creates new column and creates pivot with filter based on new column

    I have specified which sheets to use, adjusted the range for the pivot to automatically use all data (even if new columns are added), turned off screenupdating and pivot table updating in order to speed things up, and generally streamlined the code.

  10. #10
    Registered User
    Join Date
    01-19-2012
    Location
    US, United States
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro that creates new column and creates pivot with filter based on new column

    Rory,

    I noticed that your macro, though it produces the exact results, runs much, much faster. Is this because of the screen updating (essentialy your code does not show changes on screen, it just implements them)? Both were very helpful, and have been added to your reputation.

    Thanks!

    BW

  11. #11
    Registered User
    Join Date
    01-19-2012
    Location
    US, United States
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro that creates new column and creates pivot with filter based on new column

    This works like a charm! I like to see the screen updating, though not necessary. Thank you so much! Your reputation has been added to.

    thanks,

    BW

  12. #12
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro that creates new column and creates pivot with filter based on new column

    Yes, the screen updating and preventing the pivot from updating while it is being altered make the difference.

    Thanks for the rep.

+ 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