VBA - pivot table top 10 filter based on cell value

thomas819

New Member
Joined
Nov 23, 2020
Messages
36
Office Version
  1. 2019
Platform
  1. Windows
Hello all,
I am currently at lost... I can´t figure out how to do seemingly easy task of filter top n values in pivot table base on another cell value.

When I recorded macro in hope to see what I have to tweak in order to set up this filter... I was shocked... When I changed Value1 Macro didn´t run at all. I spent a few hours digging online for solution...
Unfortunately non of them fit my case...

I hope someone will help me with my problem :)

Here is my recorded macro:
Sub Top_Filter()
Range("B11").Select ´I clicked on first column in my pivot table
ActiveSheet.PivotTables("TOP").PivotFields( _
"[sumarizácia].[Celé meno].[Celé meno]").PivotFilters.Add2 Type:=xlTopCount, _
DataField:=ActiveSheet.PivotTables("TOP").CubeFields( _
"[Measures].[Súcet Prac. dni mimo práce]"), Value1:=20 ´when I changes Value:=20 to whatever number... macro didn´t run at all... I get this msg: Run-time error ´1004´
End Sub

I will be grateful for any help you can give me.
Thank you in advance.

Best regards,
Tomas
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
VBA Code:
Sub Top_Filter()
Range("B11").Select                                                                                ´I clicked on first column in my pivot table
ActiveSheet.PivotTables("TOP").PivotFields( _
"[sumarizácia].[Celé meno].[Celé meno]").PivotFilters.Add2 Type:=xlTopCount, _
DataField:=ActiveSheet.PivotTables("TOP").CubeFields( _
"[Measures].[Súcet Prac. dni mimo práce]"), Value1:=20                                             ´when I changes Value:=20 to whatever number... macro didn´t run at all... I get this msg: Run-time error ´1004´
End Sub
 
Upvote 0
Could anybody help me with this issue? I am lost... I believe, that everything I need is to somehow declare variable let say value which I will input (n) in cell A1.... Then I have no idea how to tell my pivot table to filter Top (A1.Value).

Please... I am a quite desprate to find solution to this seamingly simply problem... Simple problem at first glance... Then I find out, it is beoynd me ability.
 
Upvote 0
Please, can anybody help me? I am really in lost in VBA.
I will be grateful for any help which you can provide to me in this topic.

ActiveSheet is sheet named Calc
 
Upvote 0
For some reason this VBA still doesn't work...

Excel Formula:
Sub Makro1()

Dim x As Worksheet
Set x = ThisWorkbook.Sheets("Kalkulácie")

Dim var As Variant
var = x.Range("C1").Value

    x.PivotTables("TOP").PivotFields( _
        "[sumarizácia].[Celé meno].[Celé meno]").ClearAllFilters
    x.PivotTables("TOP").PivotFields( _
        "[sumarizácia].[Celé meno].[Celé meno]").PivotFilters.Add2 Type:=xlTopCount, _
        DataField:=x.PivotTables("TOP").CubeFields( _
        "[Measures].[Súcet Prac. dni mimo práce]"), Value1:=var
End Sub

I got this error:
Run-time error '9':
Subscrpit out of range
 
Upvote 0
Solution was surprisingly straight forward
VBA Code:
Sub Top10filter()

    ActiveSheet.PivotTables("Top").PivotFields("[sumarizácia].[Celé meno].[Celé meno]").ClearAllFilters
    ActiveSheet.PivotTables("TOP").PivotFields( _
        "[sumarizácia].[Celé meno].[Celé meno]").PivotFilters.Add2 Type:=xlTopCount, _
        DataField:=ActiveSheet.PivotTables("TOP").CubeFields( _
        "[Measures].[Súcet Prac. dni mimo práce]"), Value1:=Range("C1").Value
End Sub

I just need add ClearAllFilters clause
 
Upvote 0
Solution

Forum statistics

Threads
1,214,392
Messages
6,119,257
Members
448,880
Latest member
aveternik

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