Pivot Table VBA issue between 2016 and 2010

jdaywalt

Board Regular
Joined
Jul 18, 2012
Messages
63
I am a self taught VBA user but have done pretty well by combining code "writing" with code "recording" to maximize my abilities in Excel. Here is my issue. I have a file that I built for someone who has Excel 2010 (I have 2016). I 'recorded' a simple macro to set a value filter in a pivot table to be "less than or equal to zero". It works fine for me when I execute the macro, but when the user tries to execute within Excel 2010, she gets an error. It is getting hung up when trying to apply the value filter, and I'm wondering if there is something with the syntax when recording this macro in 2016 that isn't downwardly compatible with 2010. Is there an cleaner/simpler way for me to express what I'm trying to do (set the value filter) that would be compatible with both versions?
Code:
Sub Filter_Margin_Analysis()
    
       
    If ActiveSheet.FilterMode Then
    ActiveSheet.ShowAllData
    End If
    
  'Filter items where Units <> 0, and Margin % < Target
    
   If Range("L17").Value = "Filtered" Then
        ActiveSheet.PivotTables("Margin_Analysis").PivotFields("product_code"). _
            ClearValueFilters
        ActiveSheet.PivotTables("Margin_Analysis").PivotFields("product_code"). _
            PivotFilters.Add2 Type:=xlValueIsLessThan, DataField:=ActiveSheet. _
            PivotTables("Margin_Analysis").PivotFields(" +/- Target"), Value1:=0, Order _
            :=0
        Else
        ActiveSheet.PivotTables("Margin_Analysis").PivotFields("product_code"). _
            PivotFilters.Add2 Type:=xlValueIsLessThan, DataField:=ActiveSheet. _
            PivotTables("Margin_Analysis").PivotFields(" +/- Target"), Value1:=0, Order _
            :=0
        
        Range("L17").Value = "Filtered"
    
    End If


End Sub
 
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Through various google searches on "vba pivot filter for excel 2010" was able to pull up similar VB code to what I have, and noticed that in 2010 Excel, the only difference is that instead of "PivotFilters.Add2", it's simply "PivotFilters.Add" (no "2" on the end). After deleting the 2 in both instances in my code, the user was able to execute the macro in her 2010 version of Excel.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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
Back
Top