+ Reply to Thread
Results 1 to 13 of 13

VBA to pull back a Name if value of the data is > 0 in the pivot table

  1. #1
    Registered User
    Join Date
    01-03-2013
    Location
    Hartford CT
    MS-Off Ver
    Excel 2010
    Posts
    20

    VBA to pull back a Name if value of the data is > 0 in the pivot table

    Hey all,

    I am trying to pull back all account manager names that have a census count >0 to a different sheet in my Workbook so that I can use it as a control to populate other info on my report.

    I by no means very fluent with VBA and have run into a few problems.

    First off I have 3 slicers that control my back end pivot table and each time the pivot table updates I want my code to run to change the list of AM names.

    The current error that I am getting is Compile Error: Type Mismatch

    My second issue that I am having is trying to figure out how to pull back only the account manager name where there is a census cnt > 0

    When I try and use the code to get the data within the pivot table DataFields(i).PivotItems(j) it doesnt work and is telling me the following:

    Compile Error: Type Mismatch


    I think it has something to do with how I am defining my AMCnt but I am not sure what else to call it but Long.



    Here is my current code: (attached are 2 screen shots of my report and the source pivot that I am using)pivot table.JPGreport.JPG

    Please Login or Register  to view this content.

    Any help you can provide is greatly appreciated.

    Thanks in advance
    Last edited by alansidman; 10-18-2013 at 07:23 PM. Reason: code tags added

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Hi Marty,

    I got no compile error from this code - is there anything else??
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    01-03-2013
    Location
    Hartford CT
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Quote Originally Posted by xladept View Post
    Hi Marty,

    I got no compile error from this code - is there anything else??


    Hi xladept,

    The code does not run when the pivot table is updated by the slicer. Is there a way that I should be referencing it differently?


    Also when i created the code as just a sub routine and manually ran it in the VBA editor I was able to pull back all of the names, when I just wanted to pull back the ones that have a 1 in the census count from my pivot table.

    Hope this makes sense and thanks in advanced!

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,772

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (I have added to the thread for you. Please read our rules and abide by them in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Registered User
    Join Date
    01-03-2013
    Location
    Hartford CT
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Quote Originally Posted by alansidman View Post
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (I have added to the thread for you. Please read our rules and abide by them in the future.)
    Thank you and I apologize for the inconvenience

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Maybe:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-03-2013
    Location
    Hartford CT
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Quote Originally Posted by xladept View Post
    Maybe:

    Please Login or Register  to view this content.
    That worked! Now my code runs when the slicer is updated but I am receiving the following error:

    Unable to get the pivotitems property of the pivotfield class

    It is failing on the following line of code:

    Please Login or Register  to view this content.
    Here is a complete look of all my code with the one change:



    Please Login or Register  to view this content.

    Thanks for everyones help so far! you guys rock!

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Could it be overflow? There's no limiting code for these increments:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-03-2013
    Location
    Hartford CT
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Quote Originally Posted by xladept View Post
    Could it be overflow? There's no limiting code for these increments:
    Please Login or Register  to view this content.


    No because I put a break point in at the beginning of the For Each statement and it broke right on the next line of :

    Please Login or Register  to view this content.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Please Login or Register  to view this content.
    ???

  11. #11
    Registered User
    Join Date
    01-03-2013
    Location
    Hartford CT
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Quote Originally Posted by xladept View Post
    Please Login or Register  to view this content.

    ???

    I am trying to have the code look at the pivot table (screen shot is in the first post) where the am census cnt is >0 to pull back the corresponding am name that is associated with that respective count.

    I am not sure the way I coded it was correct. I am only going off of what I found on line with how to reference pivot tables using vba online.


    If there is a better way to do this please let me know. My VBA skills are very minimal.

    Thanks in advanced!

  12. #12
    Registered User
    Join Date
    01-03-2013
    Location
    Hartford CT
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    I ended up figuring it out.

    For those that are looking to do something similar here is my final code:


    Please Login or Register  to view this content.
    Thanks to all that responded to my post.

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to pull back a Name if value of the data is > 0 in the pivot table

    Glad you figured it out - you're welcome.

+ 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. [SOLVED] Pull data from pivot table based on date
    By djdjdj in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 05-17-2013, 03:00 AM
  2. Replies: 1
    Last Post: 03-21-2012, 10:14 AM
  3. Pull Pivot Table Data
    By Dave in forum Excel General
    Replies: 0
    Last Post: 05-23-2006, 10:15 AM
  4. Pull pivot table data
    By Dave in forum Excel General
    Replies: 1
    Last Post: 05-19-2006, 08:10 PM
  5. Vlookups in a Pivot table brining back OFFSET Data
    By tlk40us in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-20-2006, 01:55 PM

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