+ Reply to Thread
Results 1 to 2 of 2

Loop through all elements in Pivot Table

  1. #1
    Registered User
    Join Date
    05-10-2017
    Location
    Baltimore, MD
    MS-Off Ver
    2013
    Posts
    2

    Loop through all elements in Pivot Table

    Hello,

    I have a Pivot Table grouped by State abbreviation & Employee Name in one worksheet.

    The code needs to add state level data and employee level data from other worksheets to each row in this pivot table. Essentially the goals are:

    1. Retrieve state level data from another worksheet and copy to the top row for the state. Will do this once for each state.

    2. Loop through all employees for that specific state and retrieve employee level data. Copy parts of the external data (counts, employee hours, hourly rate etc) and paste in the employee's row.

    -----------

    The initial code is below:

    Set stateCode = pvt.pivotFields("STATE_CODE")
    Set acoName = pvt.pivotFields("ACO_NAME")

    For Each statePivotItem In stateCode.PivotItems
    stateCode.ClearAllFilters
    stateCode.CurrentPage = statePivotItem.Name

    MsgBox "State is " & statePivotItem.Value 'Works well

    'Copy all state level data to the appropriate cells


    'This does not work as it loops through ALL employees instead of only this state's employees

    For Each itm In fld.PivotItems

    MsgBox "Item is " & itm.Value

    'Do other stuff to copy other data based on ACO Name & State code
    Next
    Next

    -----------

    What code should be used to loop and read each element in the Pivot table? The number of rows will be variable depending on the employees but the columns will remain the same.

    Thanks!

    Frank

  2. #2
    Registered User
    Join Date
    05-10-2017
    Location
    Baltimore, MD
    MS-Off Ver
    2013
    Posts
    2

    Re: Loop through all elements in Pivot Table

    Bump.

    Any ideas on how to loop through each row in a Pivot table with two key fields and read specific elements?

    Thank you.

+ 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] How do I write a For Each Loop with random elements?
    By bananajelly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2014, 12:56 PM
  2. Counting elements in a pivot table once a filter is set
    By Zealotwraith in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-24-2013, 09:15 AM
  3. Replies: 4
    Last Post: 07-22-2013, 10:22 AM
  4. [SOLVED] Counting number of elements in a pivot table
    By Zealotwraith in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2013, 01:26 AM
  5. [SOLVED] Can a pivot table show the number of different elements for a given row label?
    By psychedelic in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 11-21-2012, 09:13 PM
  6. Pivot Table Data Elements Filtering Office 2003
    By SPenney in forum Excel General
    Replies: 1
    Last Post: 04-09-2010, 12:09 PM
  7. Pivot table-update the element in the array and stop other elements changing
    By zaalhy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2008, 10:42 AM

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