Application-defined or object-defined error

bulldaddy1

New Member
Joined
Aug 25, 2009
Messages
39
Good afternoon...

I recorded a Macro for a pivot table that turns on the Top 10 Value Filter and it works perfectly...however, if I was to click on the command button after it has already filtered the Top 10, I get a Run-time error 1004.
Is there any code that could be added to prevent this from happening?

Thank you for your time and expertise!

Sub Top10Units()
'
' Top10Units Macro
'

'
ActiveSheet.PivotTables("PivotTableUnit").PivotFields("Brand").PivotFilters. _
Add2 Type:=xlTopCount, DataField:=ActiveSheet.PivotTables("PivotTableUnit") _
.PivotFields("CY Vol"), Value1:=10
Range("J6").Select
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try clearing the existing filter before you run the current filter

VBA Code:
Sub Top10Units()
ctiveSheet.PivotTables("PivotTableUnit").PivotFields("Brand").clearAllFilters
ActiveSheet.PivotTables("PivotTableUnit").PivotFields("Brand").PivotFilters. _
Add2 Type:=xlTopCount, DataField:=ActiveSheet.PivotTables("PivotTableUnit") _
.PivotFields("CY Vol"), Value1:=10
Range("J6").Select
End Sub
 
Upvote 0
Michael M! You are genius!!!!
Thank you SO MUCH for your time and expertise...that works perfect.
Have a great weekend!
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

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