Consulting

Results 1 to 9 of 9

Thread: Sleeper: Looping through pivot (and copying data to another sheet)

  1. #1

    Sleeper: Looping through pivot (and copying data to another sheet)

    Dear community
    I have attached an example of pivot and VBA code which loops w/o problem through a pivot table and copies data, in this case for the pivotfield "Basin" into another table.
    I just would like the same but not on "Basin", but "ProductGroup".
    By changing the relevant codes, it does not work.
    I am beginner of VBA, so my apologies.

    Furthermore I would be glad to have a solution that after the looping is completed, it returns back to "(All)" in the case of existing/attached file and same (to "ProductGroup") if it loops through "ProductGroup" in the changed file.

    Thank you for any support.

    Best regards
    Heinz
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    332
    Location
    Trying to understand workbook's behavior but really lost. Where is the code that fails?

    Do need to end a copy/paste operation with:

    Application.CutCopyMode = False

    Then can set focus wherever you want.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Dear VBAX Tutor, Thank you for your reply
    I reuploaded a file (Example11bis) with changed VBA code.
    VBA code in previous file was working BUT I do not want anymore to loop through "Basin" but "ProductGroup". By changing "Basin" by "ProductGroup" VBA doesn't work anymore - with old and new code. Error in CurrentPage...
    To solve this, I ask for support. Thank you very much.
    Attached Files Attached Files

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,119
    Location
    Where does one find the pivot field "Product Group"?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,891
    You must tell us where you've cross posted to:
    https://www.excelforum.com/excel-pro...her-sheet.html
    see: https://excelguru.ca/a-message-to-forum-cross-posters/

    Try something like:
    Sub blah()
    Summary_Start = 2
    Set ProdGrp = Sheets("Pivot").PivotTables("PivotTable1").PivotFields("ProductGroup")
    Set pitms = ProdGrp.PivotItems
    For Each pitm In pitms
      pitm.Visible = True
      For Each pitm2 In pitms
        If pitm2.Name <> pitm.Name Then pitm2.Visible = False
      Next pitm2
      For j = 3 To 6
        Sheets("GAP").Range("D" & j + Grange & ":K" & j + Grange).Copy
        Sheets("Summary").Range("A" & Summary_Next + Summary_Start) = pitm.Name
        Sheets("Summary").Range("B" & Summary_Next + Summary_Start).PasteSpecial Paste:=xlPasteValues
        Grange = Grange + 3
        Summary_Start = Summary_Start + 18
      Next j
      Grange = 0
      Summary_Start = 2
      Summary_Next = Summary_Next + 1
    Next pitm
    ProdGrp.ClearAllFilters
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    332
    Location
    See post #2 for additional code edit.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Quote Originally Posted by June7 View Post
    See post #2 for additional code edit.
    Thanks a lot, code works well.

  8. #8
    Where shall I tell you the cross posting websites (in fact the one that you already found and another German website/in German)?
    https://www.clever-excel-forum.de/Th...zu-durchlaufen

  9. #9
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    332
    Location
    I it is considered a courtesy when posting to multiple forums to indicate so in original post and to provide links.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •