Pivot table show details on a summary sheet

killagori1la

New Member
Joined
Mar 31, 2012
Messages
3
I created a pivot table generated from a table that uses a source file but only takes the necessary data. I needed to sum how much a person has spent and if they go over a certain amount only then do i need to see the details so I can add to records. I didnt want to have to drill down on each criteria that met this and copy paste from new worksheets into a final report. I wrote some code that will allow the user to input the minimum amount that they are looking for and generate all the details from the pivot table onto one report worksheet. I couldnt find anything else online about doing this so now that it works for my needs, I wanted to know if this may be useful to other users.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi and Welcome to the Board,

I'd be interested in seeing how you did that.

The MrExcel.com Forum gets searched pretty often as you can see by the way the number of views for a thread continues to grow long after a question has been answered.

So if you add a post to this thread that shows the code that you developed, there's a good chance that someone will find that useful.

Thank you for offering to share that. :)
 
Upvote 0
Im new to excel and vba but this is what i came up with. I just copied and pasted from my file so not sure how it will look on board.
Code:
Sub Report()
    Dim c As Range
    Dim usr As Long
   
    Application.ScreenUpdating = False 'Turn off screen updating while this cleans up workbook'
    
    CreateReport.setsheet 'Pivot worksheet becomes active sheet'
    CreateReport.DelSheets 'Removes all irrelevant sheets'

    On Error Resume Next 'Allows macro to run if user inputs a number that causes error'
      
    usr = Application.InputBox("Minimum $ Amount to Generate Report", "Input Box", Type:=1) 'Input Box'
    
    If Not IsNumeric(usr) Then Exit Sub 'Validates input'
    If Val(usr) < 1 Then Exit Sub
    If Range("b2").Value >= usr Then 'Ensures data meets minimum criteria before generating report'
        For Each c In Range("b2:B1000") 'Loops through range of data'
            CreateReport.setsheet 'Activates Pivot worksheet for each iteration'
        
                If c.Value < usr Then 'Exits loop when data no longer meets minimum criteria'
                    Exit For
                End If
    
            c.Select
            CreateReport.showdetail 'Creates worksheet with details of selected range in pivot table'
       
     
        Next c
    
        CreateReport.CopyFromWorksheets 'Generates our report summary'
        CreateReport.Cleanup 'Removes all worksheet that were generated when showdetail was used'
    
    End If
 
    Application.ScreenUpdating = True
   

End Sub
Sub showdetail()

    Selection.showdetail = True 'Will show detail of selection in pivot table.'

End Sub
Sub setsheet()
    Dim sht As Worksheet 'Object for handling worksheets in loop

    Set sht = ActiveWorkbook.Worksheets("Pivot") 'Working in active workbook

    sht.Activate

End Sub
Sub CopyFromWorksheets() 'this sub was found online
    Dim wrk As Workbook 'Workbook object - Always good to work with object variables
    Dim sht As Worksheet 'Object for handling worksheets in loop
    Dim trg As Worksheet 'Report Worksheet
    Dim rng As Range 'Range object
    Dim colCount As Integer 'Column count in tables in the worksheets
     
    Set wrk = ActiveWorkbook 'Working in active workbook
     
    For Each sht In wrk.Worksheets
        If sht.Name = "Report" Then
            MsgBox "There is a worksheet named 'Report'." & vbCrLf & _
            "Please remove or rename this worksheet since 'Report' will be" & _
            "the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
            Exit Sub
        End If
    Next sht

     'Add new worksheet as the last worksheet
    Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
    trg.Name = "Report" 'Rename the new worksheet
    
     
    Set sht = wrk.Worksheets(1) 'Get column headers from the first worksheet
    colCount = sht.Cells(1, 255).End(xlToLeft).Column 'Column count first
     
    With trg.Cells(1, 1).Resize(1, colCount) 'Now retrieve headers, no copy&paste needed
        .Value = sht.Cells(1, 1).Resize(1, colCount).Value
        .Font.Bold = True 'Set font as bold
    End With
          
    For Each sht In wrk.Worksheets 'We can start loop
         
        If sht.Index = wrk.Worksheets.Count - 3 Then 'If worksheet in loop is the last one, stop execution (it is Report worksheet)
            Exit For
        End If
         'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
        Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
         'Put data into the Report worksheet
        trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    Next sht
     
    trg.Columns.AutoFit 'Fit the columns in Report worksheet
    
End Sub
Sub DelSheets()
    Dim ws As Worksheet 'Creates worksheet variable'

    Application.DisplayAlerts = False 'Disable displaying alerts'

    For Each ws In Worksheets 'Deletes all worksheets except the ones containing the report data
        If ws.Name <> "Pivot" And ws.Name <> "Table" And ws.Name <> "Source" Then
            ws.Delete
        End If
    Next

    Application.DisplayAlerts = True 'Enables displaying alerts
End Sub
Sub Cleanup() 'After the report is generated this deletes all the worksheets that were created
              'when the show detail was being used
    Dim ws As Worksheet

    Application.DisplayAlerts = False

    For Each ws In Worksheets
        If ws.Name <> "Pivot" And ws.Name <> "Table" And ws.Name <> "Source" And ws.Name <> "Report" Then
            ws.Delete
        End If
    Next

    Application.DisplayAlerts = True

End Sub
 [code]
[/SIZE]
 
Last edited:
Upvote 0
I dont know if i worded this posting right but if anyone interested what it does exactly i have my test file available. And any suggestions on any more efficient ways to do this are welcome. With slight modifications to the coding I think this could possibly be very useful in different applications
 
Upvote 0
Thank you for sharing your code. I sent you a private message asking you for a copy of your test workbook.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top