Hi,
I am trying to filter pivot fields and datasets through user input with multiple filters. Pivot filters works fine in isolation. I have tried different variations and could not find a solution how to make them work together. Dataset can not be filtered with 3 criterias, and I could not figure out a way to get datasets filtered.
I have user input for stating a starting number, another user input for stating an ending number and one user input for stating additional numbers. For example starting value could be 100, ending value 199 and additional numbers 255 & 265. Macro contains several subs with multiple workbooks containing pivots and datafields. I will add below the relevant parts of the macro.
I do not have much of experience with vba, so would truly appreciate if someone could help me out with this one. Have spent lots of time trying to figure it out. I tried to find solutions to these problems, but didn't find any working solutions (or maybe just didn't understand how to incorporate them on my macro ) so I am not even sure it can be done. If that is the case, I'm sure there are some workarounds to overcome these problems.
I am trying to filter pivot fields and datasets through user input with multiple filters. Pivot filters works fine in isolation. I have tried different variations and could not find a solution how to make them work together. Dataset can not be filtered with 3 criterias, and I could not figure out a way to get datasets filtered.
I have user input for stating a starting number, another user input for stating an ending number and one user input for stating additional numbers. For example starting value could be 100, ending value 199 and additional numbers 255 & 265. Macro contains several subs with multiple workbooks containing pivots and datafields. I will add below the relevant parts of the macro.
Code:
Public SalesStart As Integer
Public SalesEnd As Integer
Public SalesAdd As Integer
SalesStart = Application.InputBox(Prompt:="Enter first number of sales", Type:=1)
SalesEnd = Application.InputBox(Prompt:="Enter last number of sales", Type:=1)
SalesAdd = Application.InputBox(Prompt:="Enter the numbers you want to filter" & vbNewLine & "Seperated by a comma (,)")
Public Function IsInArray(stringToBeFound As Long, arr As Variant) As Boolean
Dim i
For i = LBound(arr) To UBound(arr)
If arr(i) = stringToBeFound Then
IsInArray = True
Exit Function
End If
Next i
IsInArray = False
End Function
Dim Korrews As Worksheet
Dim arr() As String
Dim pi As PivotItem
Set Korrews = Sheets("Correlations")
Korrews.PivotTables("SalesCorrelations").AllowMultipleFilters = True
If SalesStart <> False Then
'This works on its own. Was not able to make them work together
Korrews.PivotTables("SalesCorrelations").PivotFields("Number").PivotFilters.Add2 _
Type:=xlCaptionIsBetween, Value1:=SalesStart, Value2:=SalesEnd
If SalesAdd <> False Then
'This works on its own. Was not able to make them work together
SalesAdd = Trim(Replace(SalesAdd, " ", vbNullString))
arr = Split(SalesAdd, ",")
With Korrews.PivotTables("SalesCorrelations").PivotFields("Number")
For Each pi In .PivotItems
If Not SalesAdd = vbNullString Then
pi.Visible = IIf(IsInArray(pi.Name, arr), True, False)
Else
pi.Visible = True
End If
Next pi
End With
Else
End If
Else
Korrews.PivotTables("SalesCorrelations").PivotFields("Number").PivotFilters.Add2 _
Type:=xlCaptionIsBetween, Value1:="100", Value2:="199"
End If
'Tried filtering datasets by looping the filter field and hiding entirerow if value is not SalesAdd value. Just ended up crashing Excel multiple times. Below is the part that does work. Would need to filter values between SalesStart and SalesEnd and add values of SalesAdd.
If SalesStart <> False Then
ActiveSheet.Range("A:A").AutoFilter field:=1, Criteria1:=">=" & SalesStart _
, Operator:=xlAnd, Criteria2:="<=" & SalesEnd
I do not have much of experience with vba, so would truly appreciate if someone could help me out with this one. Have spent lots of time trying to figure it out. I tried to find solutions to these problems, but didn't find any working solutions (or maybe just didn't understand how to incorporate them on my macro ) so I am not even sure it can be done. If that is the case, I'm sure there are some workarounds to overcome these problems.