+ Reply to Thread
Results 1 to 3 of 3

Macro fails when filter is already applied

  1. #1
    Registered User
    Join Date
    03-02-2016
    Location
    NC, USA
    MS-Off Ver
    2013
    Posts
    6

    Macro fails when filter is already applied

    I recorded a macro that applies a top 10 filter to a pivot field in a pivot table. It works well if the pivot table is unfiltered when the macro is run, but if a user hits the button when the top 10 filter is already being applied, I get the:

    Run-time error '1004':
    Application-defined or object-defined error.

    Ideally, if the user selects the macro button and the top 10 filter is already being applied, it would just do nothing...

    Here is the macro:
    Sub Chart1_Top_10()
    '
    ' Chart1_Top_10 Macro
    '

    '
    Sheets("Chart1").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("SENDER_ID").PivotFilters. _
    Add2 Type:=xlTopCount, DataField:=ActiveSheet.PivotTables("PivotTable1"). _
    PivotFields("Total Messages"), Value1:=10
    Sheets("Dashboard").Select
    End Sub

    When I debug it highlights the bolded rows above.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro fails when filter is already applied

    Hi,

    Try putting the code inside an

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    or preface your macro with some code that turns the filter off first.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    03-02-2016
    Location
    NC, USA
    MS-Off Ver
    2013
    Posts
    6

    Re: Macro fails when filter is already applied

    Thanks. The code block with the if statement did not work, but adding code to clear filters worked.

    I added the bold face code below:
    Please Login or Register  to view this content.
    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. View next visible row after filter has been applied
    By Wales MB in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-18-2015, 10:21 AM
  2. [SOLVED] Checking if filter is applied
    By Niclal in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-09-2014, 07:48 AM
  3. [SOLVED] Need Macro to Select All Files in a Column After Filter Applied
    By Gard5096 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-12-2012, 05:33 PM
  4. Call macro when filter is applied?
    By JP Romano in forum Excel General
    Replies: 6
    Last Post: 06-24-2011, 07:56 AM
  5. Replies: 0
    Last Post: 04-18-2011, 02:22 PM
  6. [SOLVED] how do i identify first and last row once filter applied
    By pete the greek in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-21-2006, 12:25 PM
  7. [SOLVED] In Excel, why sort function fails when applied to a list of date?
    By Excel heavy user in forum Excel General
    Replies: 1
    Last Post: 01-18-2005, 03:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1