Grouping last 10 by dates

robbymp

New Member
Joined
Dec 2, 2018
Messages
5
Hi, I been struggling with this for awhile, I'm trying to group the last 10 records by date for each field, totals only, also if I add another date, I need it to keep a running total by last 10, I can do this in Microsoft Access using Top Select 10. Is this possible in excel or is there a link to an example, I can follow, I tried doing Pivot tables, couldn't get results.

Thank You
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Apologies, try this:

Just update the pivot table number and field name.

Code:
Sub ShowLast10Days()
Dim pi As PivotItem
Dim lLoop As Long
Dim pt As PivotTable
Dim lCount As Long
Dim lDays As Long


    lDays = 10


    Application.ScreenUpdating = False
    Set pt = ActiveSheet.PivotTables("PivotTable2")
        
            For Each pi In pt.PivotFields("Date").PivotItems
            On Error Resume Next
                pi.Visible = False
            Next pi
        
        
        With pt.PivotFields("Date")
            For lLoop = .PivotItems.Count To 1 Step -1
                .PivotItems(lLoop).Visible = True
                lCount = lCount + 1
                If lCount = lDays Then Exit For
            Next lLoop
        End With
        
    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Yes, Excel can do a top 10 exactly the same as Access. You can use the same SQL - such as in a query (table), pivot table. Or via VBA additionally with recordset type approaches.

So SQL as in Access, something like,
SELECT TOP 10 fields
FROM source
WHERE critieria
GROUP BY fields if required
ORDER BY somefield DESC
 
Upvote 0

Forum statistics

Threads
1,216,153
Messages
6,129,172
Members
449,490
Latest member
TheSliink

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