ASKER
ASKER
Option Explicit
Sub Top3(sSource As String)
On Error Resume Next
Dim WS As Worksheet
Dim Pvt As PivotTable
Dim pf As PivotField
Dim df As PivotField
Dim Pfilter As PivotFilter
'MsgBox sSource
Application.ScreenUpdating = False
Set WS = ActiveSheet
For Each Pvt In WS.PivotTables
If Pvt.Name = "PivotTable3" Then
'---> Clear All Filters and fields and Data
Pvt.ClearAllFilters
For Each pf In Pvt.PivotFields
For Each df In Pvt.DataFields
df.Orientation = xlHidden
Next df
If Not pf Is Nothing Then pf.Orientation = xlHidden
Next pf
'---> Create Field as selected
Pvt.AddFields RowFields:=sSource
Set pf = Pvt.PivotFields(sSource)
pf.LabelRange = sSource
Pvt.AddDataField Pvt.PivotFields("Quantity")
Set df = Pvt.DataFields(1)
df.LabelRange = "Total Quantity"
df.LabelRange.HorizontalAlignment = xlRight
df.NumberFormat = "#,##0 "
'---> Filter Top N
'pvt.PivotFields(sSource).PivotFilters.Add Type:=xlTopCount, DataField:=df, Value1:=3, Order:=xlDescending
pf.PivotFilters.Add Type:=xlTopCount, DataField:=df, Value1:=Val(Sheets("Pivot").ComboBox1.Value), Order:=xlDescending
pf.AutoSort Order:=xlDescending, field:=df
Pvt.RowGrand = True
End If
Next Pvt
Application.ScreenUpdating = True
End Sub
ASKER
ASKER
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
Can you please post a sample workbook ?
Gowflow