+ Reply to Thread
Results 1 to 15 of 15

vba code to reference value in Pivot table

  1. #1
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    vba code to reference value in Pivot table

    Hi All

    I have a Pivot table that I need to reference to populate a TextBox. The TextBox value must become 6795 taken from PivotTable. Very new to this so please help.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    Nevermind...Guess it would be easier just to reference the cell in the sheet

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-21-2013
    Location
    Wiltshire, UK
    MS-Off Ver
    365 Version 2210 Build 16.0.15726.20070
    Posts
    72

    Re: vba code to reference value in Pivot table

    What if you have more or less rows in the pivottable? the total may not be at B11? Have a look at the Getpivotdata() function.
    Isskint, i get satisfaction out of helping others

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    Hi All

    I spoke to soon. How about allowing me to populate my textbox with today total or allowing a choice of date....Any ideas. I have attached my file
    Attached Files Attached Files

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    Hi isskint

    I am lost...It's greek to me...Been on web and just can't understand.

  6. #6
    Registered User
    Join Date
    06-21-2013
    Location
    Wiltshire, UK
    MS-Off Ver
    365 Version 2210 Build 16.0.15726.20070
    Posts
    72

    Re: vba code to reference value in Pivot table

    have a look at this link https://support.office.com/en-gb/art...e-3af55960761f

    So if you have a field called Jedi then Getpivotdata("Jedi",A1) will return the sum of Jedi (where A1 is in the pivot table)
    Last edited by isskint; 08-09-2016 at 07:12 AM.

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    I'm sorry isskint

    Maybe I'm having a blonde moment...I just can't comprehend.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-21-2013
    Location
    Wiltshire, UK
    MS-Off Ver
    365 Version 2210 Build 16.0.15726.20070
    Posts
    72

    Re: vba code to reference value in Pivot table

    Where is the textbox? on the sheet or on a form?

    The way i would do this is to have the Getpivotdata formula in a cell and reference that cell for textbox 5.
    If you want to do it through VBA then the correct use is something like;
    Please Login or Register  to view this content.
    so
    Please Login or Register  to view this content.
    would return todays data

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    The Textbox is on a Userform. i am busy creating a POS system. I need to be able to click on comand button to generate value in textbox i.e. Daily sales, Monthly sales and grand total.

    i have attached an updated file.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    06-21-2013
    Location
    Wiltshire, UK
    MS-Off Ver
    365 Version 2210 Build 16.0.15726.20070
    Posts
    72

    Re: vba code to reference value in Pivot table

    OK, so the code you would want for each of the buttons is as below;
    Please Login or Register  to view this content.

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    Awesome, thanks so much. Is there a way to change the following line of code to get a monthly total i.e. Current month to date

    Please Login or Register  to view this content.

  12. #12
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    Hi All

    Can anyone perhaps assist with above code.

  13. #13
    Registered User
    Join Date
    06-21-2013
    Location
    Wiltshire, UK
    MS-Off Ver
    365 Version 2210 Build 16.0.15726.20070
    Posts
    72

    Re: vba code to reference value in Pivot table

    I do not think you can with GetPivotData.

    If you always SPECIFICALLY want current month to date you could calculate the date at end of last month then use GetPivotData to get the total on that date THEN deduct that from pvtTable.GetPivotData("Sum of Total", "Date", Date).

    The better approach would be a separate pivot where the dates are grouped by month OR add an extra column to the table giving you month/year and use that in an extra pivot table.

  14. #14
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    Hi isskint

    Thanks for feedback. Think I will use the option of creating another Pivot table.
    By the way...The below code for daily total all of a sudden throws an runtime 1004 error Application defined or Object defined error and I have no idea why.
    Please Login or Register  to view this content.

  15. #15
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: vba code to reference value in Pivot table

    Not to worry... Sorted all out including Monthly....Thanks isskint.
    My Headings of Pivot Table were incorrect.

+ 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. Replies: 0
    Last Post: 07-14-2016, 03:51 PM
  2. [SOLVED] VBA code to turn data into pivot table and then into pivot chart
    By Faintkitara in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-25-2015, 09:05 PM
  3. Replies: 5
    Last Post: 06-17-2014, 10:16 AM
  4. Replies: 2
    Last Post: 05-24-2013, 03:58 PM
  5. How to Reference a Pivot Table
    By uglion5 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-23-2011, 10:18 AM
  6. Pivot Table and Cell Reference
    By cmrluvitlevit in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-14-2011, 11:56 AM
  7. [SOLVED] VBA Code for a pivot table to open database and to reference table current page
    By Pete Straman Straman via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-21-2005, 12:06 AM

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