+ Reply to Thread
Results 1 to 8 of 8

Pivot Table - Looping Through Pivot Fields

Hybrid View

  1. #1
    Registered User
    Join Date
    10-06-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Question Pivot Table - Looping Through Pivot Fields

    Hey Guy and Dolls,

    It’s my first time here, so bear with me if I haven't done something I was suppose to do while posting. Any feedback would be greatly appreciated.

    First off, I would like to thank the person/people who created this site so people like me can ask questions. Also I would like to thank the volunteers that use their spare time (or sometimes work time ) to answer my questions. I hope that in the future I will be among these warriors of people.

    Ok my question, it’s in regards to pivot table pivot fields.

    I have created a macro that will create a pivot table in a specified worksheet and fill in the row and data fields. The problem is I need the data field to include multiple pivot fields that meet a specified criteria.

    Is it possible the macro could loop through all available pivot fields, looking for the word 'Points' and then putting the matching pivot field into the data field?

    To explain in more detail I have attached a sample workbook with my existing macro and the following worksheets:
    1. Data - Contains the data that the pivot table uses. This is generic.
    2. Pivot - This is where the macro creates the pivot table. This is generic.
    3. CorrectPivot - This shows a pivot table in which I would like the macro to create.

    I hope I was as clear as possible.

    Kind Regards

    Mini12
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    09-18-2008
    Location
    india
    Posts
    158

    Re: Pivot Table - Looping Through Pivot Fields

    Hi Mini,

    Please find attached.

    Regards,
    Shekar Goud.
    Attached Files Attached Files

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Pivot Table - Looping Through Pivot Fields

    Mini12,

    I think in fact you want something a little more dynamic - ie code along the lines of:

    Public Sub MakePivotTable()
    Dim pt As PivotTable, ptField As PivotField
    Dim WSD As Worksheet, PTOutput As Worksheet
    Dim PTCache As PivotCache
    Dim PRange As Range
    Dim finalRow As Long, finalCol As Long
    Set WSD = Worksheets("Data")
    Set PTOutput = Worksheets("Pivot")
    With WSD
        Set PRange = .Range(.Cells(1, "A"), .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))
    End With
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)
    Set pt = PTCache.CreatePivotTable(TableDestination:=Sheets.Add.Cells(3, 1), TableName:="SamplePivot")
    With pt
        .ManualUpdate = True
        .AddFields RowFields:=Array("ID")
        For Each ptField In .PivotFields
            If InStr(UCase(ptField.Name), " POINTS") Then
                .AddDataField ptField, , xlSum
            End If
        Next ptField
        .ManualUpdate = False
    End With
    End Sub
    The above will create the PT and iterate all fields adding any containing " Points" to the Data Field section.

    Note in the above for testing I altered the destination of the PT to be a new sheet created at run-time (change as necessary).

  4. #4
    Registered User
    Join Date
    10-06-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Question Re: Pivot Table - Looping Through Pivot Fields

    Hey Shekar,

    Thanks for answering the question, the only problem is I cannot add the pivot fields individually. This is because I am trying to make this code universal where it can be used on multiple workbooks with many different number of 'Points' fields.

    For example, sample1.xls has 2 fields that has 'Points' in the field name, but sample2.xls has 12 fields with 'Points' in the field name.

    So to make it universal I need the code to loop through all pivot fields looking for the word 'Points' within the fieldname and then add it to the data field.

    I have tried to create this code but failed, this is how far I got:

    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    
    For Each pf In pt.PivotFields
        
        If InStr(pf.Value, "Points") = 1 Then
        
            'Do Something
    
        Else
    
            Exit Sub
            
        End If
    
    Next pf
    Does anyone have any ideas?

    Kind Regards

    mini12

  5. #5
    Registered User
    Join Date
    10-06-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Thumbs up Re: Pivot Table - Looping Through Pivot Fields

    Wow that was fast, got a reply before I posted a reply.

    Is there mindreaders here?

  6. #6
    Registered User
    Join Date
    10-06-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Lightbulb Re: Pivot Table - Looping Through Pivot Fields

    Thanks, DonkeyOte & shekar goud, you were great help.

    I can't believe I was that close to solving the solution myself. Thank you DonkeyOte for filling the missing pieces of the puzzle.

    I just then made three minor adjustments, which are as follows:

    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    
    For Each pf In pt.PivotFields
    
        If InStr(UCase(pf.Name), "POINTS") Then
    
            pt.AddDataField pf, , xlSum
    
        End If
    
    Next pf
    Bingo it works!

    Problem solved.

    Kind Regards

    mini12

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Pivot Table - Looping Through Pivot Fields

    Just FYI, there is no need to use a separate function to convert the name to upper case, since InStr has an argument to specify whether the comparison should be case sensitive:
    If InStr(1, pf.Name, "POINTS", vbTextCompare) Then
    FWIW.
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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