Pivot table Filter by data range values defined in userform

Daniel Vieira

New Member
Joined
Feb 5, 2016
Messages
1
Hello everyone,

I am new here, so If I make a mistake I apologize in advance.

I have been trying to get some code to filter my pivot table with a data range that is provided by userform. In the Userform I add the beginning and final month and year (being n1+m1 the initial date and n2 + m2 the final dates)

However when I run the code, it does filter throughout the dates, but randomnly.
As an example, if I run the code to get me the dates between 2/2014 and 6/2015, it selects some dates that are 1/2014 and unselects some that are inbetween the range.

Can someone help me with this? I also know that the code is probably very inefficient, but I am new at this, so give me a discount!

Code:
 Dim n1 As StringDim n2 As String
Dim m1 As String
Dim m2 As String
Dim PI As PivotItem
n1 = ComboBox3.Value
n2 = ComboBox5.Value
m1 = ComboBox4.Value
m2 = ComboBox6.Value
'n1 and n2 as years
'm1 and m2 as months
Sheets("MasterPivotTable").PivotTables("PivotTable1").PivotFields("Data (US)").EnableMultiplePageItems = TrueSheets("MasterPivotTable").PivotTables("PivotTable1").PivotFields("Data (US)").ClearAllFilters
If ComboBox3.ListCount = 0 And ComboBox5.ListCount = 0 Then
   For Each PI In Sheets("MasterPivotTable").PivotTables("PivotTable1").PivotFields("Data (US)").PivotItems
            PI.Visible = True
    Next
ElseIf ComboBox3.ListCount > 0 And ComboBox5.ListCount > 0 Then
  If n1 = n2 Then
   For Each PI In Sheets("MasterPivotTable").PivotTables("PivotTable1").PivotFields("Data (US)").PivotItems
        If PI.Value <> "(blank)" Then
           If Year(PI.Value) = n1 And Month(PI.Value) >= m1 And Month(PI.Value) <= m2 Then
             PI.Visible = True
           ElseIf PI.Visible = True Then
             PI.Visible = False
           Else
             PI.Visible = False
           End If
        End If
    Next
  ElseIf n1 <> n2 Then
   For Each PI In Sheets("MasterPivotTable").PivotTables("PivotTable1").PivotFields("Data (US)").PivotItems
        If PI.Value <> "(blank)" Then
           If Year(PI.Value) = n1 And Month(PI.Value) >= m1 Then
             PI.Visible = True
           ElseIf Year(PI.Value) > n1 And Year(PI.Value) < n2 Then
             PI.Visible = True
           ElseIf Year(PI.Value) = n2 And Month(PI.Value) <= m2 Then
             PI.Visible = True
           Else
             PI.Visible = Flase
           End If
        End If
    Next
   End If
Else
  'do nothing
End If

Thank you very much

Daniel
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,215,172
Messages
6,123,443
Members
449,100
Latest member
sktz

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