0

I have the following sub:

Sub test()

DeviceModel = "123 Model_name"

        ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release").AutoSort _
        xlDescending, "Count of Device ID"

        ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release"). _
        PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
        PivotTables(DeviceModel).PivotFields("Count of Device ID"), Value1:=10

End Sub

This part works:

        ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release").AutoSort _
        xlDescending, "Count of Device ID"

But the second part doesn't:

        ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release"). _
        PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
        PivotTables(DeviceModel).PivotFields("Count of Device ID"), Value1:=10

If I replace (DeviceModel) with ("123 Model_name") it works as expected:

        ActiveSheet.PivotTables("123 Model_name").PivotFields("Baseline Release"). _
        PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=ActiveSheet. _
        PivotTables("123 Model_name").PivotFields("Count of Device ID"), Value1:=10

It says: "application defined or object defined error"

I have done extensive googling and still cant figure out why it doesn't work with the variable instead of the actual table name.

3
  • Does a with ActiveSheet.PivotTables(DeviceModel).PivotFields("Baseline Release") have a similar result? Commented Apr 17, 2015 at 13:22
  • Yes, same result when using With Commented Apr 17, 2015 at 13:25
  • Perhaps excel is being a jerk about the variable not being declared first. Try adding Dim DeviceModel As String before you assign it a value. Commented Apr 17, 2015 at 13:38

1 Answer 1

0

I found an answer in Excel VBA - Pivot Table with Multiple Value Filters

Dim pvt As PivotTable
Set pvt = ActiveSheet.PivotTables(DeviceModel)

With pvt.PivotFields("Baseline Release")
    .ClearAllFilters
    .PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=pvt.PivotFields("Count of Device ID"), Value1:=10
End With
Sign up to request clarification or add additional context in comments.

Comments

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.