+ Reply to Thread
Results 1 to 6 of 6

VBA help needed to capture Error and Remove Pivot if error exist else resume

Hybrid View

Tescatlipoca VBA help needed to capture... 11-18-2014, 02:49 PM
Bernie Deitrick Re: VBA help needed to... 11-18-2014, 04:17 PM
Tescatlipoca Re: VBA help needed to... 11-18-2014, 04:54 PM
Bernie Deitrick Re: VBA help needed to... 11-18-2014, 05:17 PM
Tescatlipoca Re: VBA help needed to... 11-18-2014, 05:23 PM
Tescatlipoca Re: VBA help needed to... 11-18-2014, 05:46 PM
  1. #1
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    VBA help needed to capture Error and Remove Pivot if error exist else resume

    Hi I am no programmer. By reading threads and such I created a spreadsheet which has multiple macros. Basically a report generator.

    Excel version 2010

    Current Setup:
    On Load () I am forcing pivot tables to have certain filtering.

    Problem:
    When that filter is not available I am getting a VBA error. Or if I resume on Err. Filter is selected as whatever is available next (creates confusion on reporting)

    Proposed Resolution:

    I think I need to come up with a way where I check if error exist or not and move next. If exists I would like to remove the pivot table and move next, if no error than resume next.

    However, I have no clue how to achieve this. Would really appreciate guidance.

    Thank you!
    Last edited by Tescatlipoca; 11-18-2014 at 05:46 PM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,391

    Re: VBA help needed to capture Error and Remove Pivot if error exist else resume

    If you post your code, that would be helpful. But you could try code like this with multiple error handlers, where you could customize the response to errors.

    Sub PivotTableStuff()
        Dim PT As PivotTable
    
       On Error GoTo ErrHandler1
        
        Set PT = ActiveSheet.PivotTables(1)
        'code using PT
        PT.PivotFields("Category 1").PivotItems("Item 2").Visible = True
        
    Err1Resume:
        
        On Error GoTo ErrHandler2
        Set PT = ActiveSheet.PivotTables(2)
        
        'code using PT
        PT.PivotFields("Category X").PivotItems("Item 3").Visible = True
    
    Err2Resume:
        Exit Sub
        
    ErrHandler1:
        MsgBox "error in 1"
        PT.ClearTable
        Resume Err1Resume
        
    ErrHandler2:
        MsgBox "error in 2"
        PT.ClearTable
        Resume Err2Resume
        
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA help needed to capture Error and Remove Pivot if error exist else resume

    Quote Originally Posted by Bernie Deitrick View Post
    If you post your code, that would be helpful. But you could try code like this with multiple error handlers, where you could customize the response to errors.

    Sub PivotTableStuff()
        Dim PT As PivotTable
    
       On Error GoTo ErrHandler1
        
        Set PT = ActiveSheet.PivotTables(1)
        'code using PT
        PT.PivotFields("Category 1").PivotItems("Item 2").Visible = True
        
    Err1Resume:
        
        On Error GoTo ErrHandler2
        Set PT = ActiveSheet.PivotTables(2)
        
        'code using PT
        PT.PivotFields("Category X").PivotItems("Item 3").Visible = True
    
    Err2Resume:
        Exit Sub
        
    ErrHandler1:
        MsgBox "error in 1"
        PT.ClearTable
        Resume Err1Resume
        
    ErrHandler2:
        MsgBox "error in 2"
        PT.ClearTable
        Resume Err2Resume
        
    End Sub
    Thank you Bernie, I am pasting the section of the code where I force the filters.

    Sub Fix_Pivot()
    
    On Error Resume Next
    
    Sheets("PSD").Select
       'Update table 7 Number of PSDs scheduled by Month
            ActiveSheet.PivotTables("PivotTable7").PivotFields("Adhoc").CurrentPage = _
            "No"
            ActiveSheet.PivotTables("PivotTable7").PivotFields("Type").CurrentPage = _
            "PSD Inspection"
        'Update table 8 Number of PSDs scheduled by Month
            ActiveSheet.PivotTables("PivotTable8").PivotFields("Adhoc").CurrentPage = _
            "No"
            ActiveSheet.PivotTables("PivotTable8").PivotFields("Type").CurrentPage = _
            "PSD Inspection"
        'Update table 9 Number of PSDs overdue by Month
            ActiveSheet.PivotTables("PivotTable9").PivotFields("Type").CurrentPage = _
            "PSD Inspection"
            ActiveSheet.PivotTables("PivotTable9").PivotFields("Adhoc").CurrentPage = "No"
        'End of PSD tables
    
    Sheets("Maint").Select
        'Update table 2 Number of Maintenance Activities by Month
            ActiveSheet.PivotTables("PivotTable2").PivotFields("Type").CurrentPage = _
            "Maintenance"
            ActiveSheet.PivotTables("PivotTable2").PivotFields("Adhoc").CurrentPage = "No"
        'Update table 3 Number of Maintenance Activity Results by Month
            ActiveSheet.PivotTables("PivotTable3").PivotFields("Type").CurrentPage = _
            "Maintenance"
            ActiveSheet.PivotTables("PivotTable3").PivotFields("Adhoc").CurrentPage = "No"
       'Update table 4 Number of Maintenance Activities Overdue by Month.
            ActiveSheet.PivotTables("PivotTable4").PivotFields("Type").CurrentPage = _
            "Maintenance"
            ActiveSheet.PivotTables("PivotTable4").PivotFields("Adhoc").CurrentPage = "No"
         'End of Maintenance tables
    Sheets("AdHoc").Select
        'Update table 5 Number of Adhov Activities by Month.
            ActiveSheet.PivotTables("PivotTable5").PivotFields("Adhoc").CurrentPage = _
            "IsAdhoc"
            
        'Update table 5 Number of Adhoc Activity Results by Month.
            ActiveSheet.PivotTables("PivotTable6").PivotFields("Adhoc").CurrentPage = _
            "IsAdhoc"
         'End Update Filters
    
    End Sub
    On your code above, how do I go to the next pivot table, as seen from my code I have 8 pivot tables.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,391

    Re: VBA help needed to capture Error and Remove Pivot if error exist else resume

    This is how I would handle the first 3 (7 through 9)

    Sub Fix_Pivot2()
        Dim PT As PivotTable
        Dim i As Integer
        
        With Sheets("PSD")
            'Update table 7, 8, and 9 Number of PSDs scheduled by Month
            On Error GoTo ErrH7
            For i = 7 To 9
                Set PT = .PivotTables("PivotTable" & i)
                
                PT.PivotFields("Adhoc").CurrentPage = _
                "No"
                PT.PivotFields("Type").CurrentPage = _
                "PSD Inspection"
    Resume7:
            Next i
        End With
        Exit Sub
        
    ErrH7:
        PT.ClearTable
        Resume Resume7
        
    End Sub

  5. #5
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA help needed to capture Error and Remove Pivot if error exist else resume

    Thank you for your help!

  6. #6
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA help needed to capture Error and Remove Pivot if error exist else resume

    Just an update tested, and works like a charm! thank you for your help!

+ 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. Error pops up on line On Error Resume Next
    By Erusso in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-20-2012, 04:57 PM
  2. Error 1004 impossible to capture with On Error go To
    By RagonichaFulva in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2012, 12:10 PM
  3. Resume on Error?
    By dan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2006, 02:10 PM
  4. [SOLVED] on error resume next
    By filo666 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-26-2006, 02:45 PM
  5. On Error Resume Next
    By Jasper in forum Excel General
    Replies: 0
    Last Post: 05-10-2005, 11:06 AM

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