Results 1 to 4 of 4

Thread: Pivot filter by each item and copy to new sheet

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,886
    Location
    Something like this


    Option Explicit
    
    
    Sub MakeSheets()
        Dim oPivotTable As PivotTable
        Dim oPageField As PivotField
        Dim oPageItem As PivotItem
        Dim aryPageItems() As String
        Dim cntPageItems As Long, idxPageItems As Long
    
    
        If ActiveSheet.PivotTables.Count = 0 Then Exit Sub
        Set oPivotTable = ActiveSheet.PivotTables(1)
        
        If oPivotTable.PageFields.Count = 0 Then Exit Sub
        Set oPageField = oPivotTable.PageFields(1)
    
    
        For Each oPageItem In oPageField.PivotItems
            With oPageItem
                If .Visible Then
                    cntPageItems = cntPageItems + 1
                    ReDim Preserve aryPageItems(1 To cntPageItems)
                    aryPageItems(cntPageItems) = .Value
                End If
            End With
        Next
    
    
    '    Application.ScreenUpdating = False
    
    
        On Error Resume Next
        Application.DisplayAlerts = False
        For idxPageItems = LBound(aryPageItems) To UBound(aryPageItems)
            Worksheets(aryPageItems(idxPageItems)).Delete
        Next idxPageItems
        Application.DisplayAlerts = True
        On Error GoTo 0
        
        oPivotTable.ShowPages PageField:=oPageField.Value
    
    
        For idxPageItems = LBound(aryPageItems) To UBound(aryPageItems)
            With Worksheets(aryPageItems(idxPageItems))
                .Activate
                
                Application.StatusBar = ActiveSheet.Name
                
                DoEvents
                
                .PivotTables(1).TableRange2.Copy
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                
                .Cells(1, 1).Resize(oPivotTable.PageFields.Count + 1, 1).EntireRow.Delete
                
                .Range("A2").Select
                With ActiveWindow
                    .SplitColumn = 0
                    .SplitRow = 1
                End With
                ActiveWindow.FreezePanes = True
                
                .Cells(1, 1).CurrentRegion.Rows(1).Interior.ColorIndex = 15
                .Cells(1, 1).CurrentRegion.Font.Bold = True
            End With
        Next idxPageItems
        
        Application.StatusBar = False
        Application.ScreenUpdating = True
    
    
        MsgBox "Done"
        
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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