Application-defined or object-defined error

bulldaddy1

New Member
Joined
Aug 25, 2009
Messages
36
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,120
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 

bulldaddy1

New Member
Joined
Aug 25, 2009
Messages
36
Michael M! You are genius!!!!
Thank you SO MUCH for your time and expertise...that works perfect.
Have a great weekend!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,498
Messages
5,636,665
Members
416,935
Latest member
Atulcp

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
Top