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!
Thank you very much
Daniel
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