2

I am trying to make a macro that will automaticly adjust the filters for several pivot tables based on user input, however if the user puts in something that isn't avaialble the code produces an error when trying to apply the filter.

Is there anyway to check which filters are available to select?

Example: One pivot table has three filters available (Year, Month, Type [Complaint, Praise, Both]) but if during a month there weren't any complaints then there is an error.

Code:

With PTable
    .PivotFields("Year").CurrentPage = Y
    .PivotFields("Month").CurrentPage = M
    .PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
End With
4
  • Short and Dirty way... Sandwich that line between On Error Resume Next and On Error GoTo 0 Commented Feb 5, 2014 at 20:26
  • If you're in Excel 2010 or later, check into 'Slicers' -they allow you to use one filter to modify numerous pivot tables. This doesn't solve the VBA issue, but might be a simpler way to get at what you need. Commented Feb 5, 2014 at 20:32
  • Thanks guys. I was thinking about your method Siddharth Rout, but do you know if there is a way to check if the filter got applied. I would need to notify the user if a filter didn't get applied. Commented Feb 5, 2014 at 20:36
  • use err.clear just before the line, and check err.Number after. 0 is no error, anything else is an error Commented Feb 5, 2014 at 20:40

1 Answer 1

3

Further to my comments, try this

With PTable
    .PivotFields("Year").CurrentPage = Y
    .PivotFields("Month").CurrentPage = M
    On Error Resume Next
    .PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
    If Err.Number <> 0  Then
        Msgbox "Filter Didn't get applied"
    End If
    On Error GoTo 0
End With
Sign up to request clarification or add additional context in comments.

1 Comment

@Sid: Is there a way to actually check the value? I can get by with error catching, but I'd prefer validation if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.