0

I'm pretty new to VBA so I'm looking for some help on setting up a macro that will add the values from a specific range to an array and then loop through that array to update the filters on a pivot table. However not every value might be set. An example of this would be that only 4 values are set out of a 100 so it would only loop through it 4 times and update the filter with those 4 values. I have never worked with for loops before so any guidance on this would be extremely helpful.

My code so far:

Sub Update_Filters()

    Dim PortfolioCodes As Variant
    PortfolioCodes = Sheets("Configuration Sheet").Range("C7:C45").Value

    Sheets("List").PivotTables("List").PivotFields( _
        "[Portfolio].[Portfolio Code].[Portfolio Code]").VisibleItemsList = Array( _
        "[Portfolio].[Portfolio Code].&[ABC1]", "[Portfolio].[Portfolio Code].&[ABC2]", _
        "[Portfolio].[Portfolio Code].&[XYZ1]", "[Portfolio].[Portfolio Code].&[XYZ2]")
End Sub
1
  • I don't get it, you want your procedure to show the data for all 4 values at the same time or filter 1 at a time and do something? Commented Nov 26, 2019 at 16:11

1 Answer 1

1

Here, the two possible answers to your question:

Option Explicit
Sub Update_Filters()

    'this code will filter one portfolio code at a time
    Dim LastRow As Long
    With ThisWorkbook.Sheets("Configuration Sheet")
        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        Dim PortfolioCodes As Variant
        PortfolioCodes = .Range("C7:C" & LastRow).Value
    End With

    For i = LBound(PortfolioCodes) To UBound(PortfolioCodes)
        ThisWorkbook.Sheets("List").PivotTables("List").PivotFields( _
        "[Portfolio].[Portfolio Code].[Portfolio Code]").VisibleItemsList = _
            Array("[Portfolio].[Portfolio Code].&[" & PortfolioCodes(i) & "]")
        'some code
    Next i

End Sub
Sub Update_FiltersALL()

    'This code will filter all the portfolio codes in your range
    With ThisWorkbook.Sheets("Configuration Sheet")

        Dim LastRow As Long
        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

        Dim i As Long
        i = 1

        ReDim PortfolioCodes(1 To LastRow - 6) As String
        Dim C As Range
        For Each C In .Range("C7:C" & LastRow)
            If Not IsEmpty(C) Then
                PortfolioCodes(i) = "[Portfolio].[Portfolio Code].&[" & C.Value & "]"
                i = i + 1
            End If
        Next C
    End With

    ThisWorkbook.Sheets("List").PivotTables("List").PivotFields( _
    "[Portfolio].[Portfolio Code].[Portfolio Code]").VisibleItemsList = PortfolioCodes

End Sub
Sign up to request clarification or add additional context in comments.

7 Comments

I'm trying to use the second version of the code you provided which updates all, however it's throwing a subscript out of range error. Is there something that I'm over looking?
@Gil when trying to filter? might be some empty values on the range? or some codes which aren't on the olap?
Yes there are empty values in the range which is why I want the range to stop reading at the point where the next cell is empty
@Gil try my edit.. it will ignore empty cells and loop only to the last row (will ignore empty cells that might be in between your range)
I'm getting a new error "Compile Error: Constant expression required", which is highlighting the LastRow in Dim PortfolioCodes(1 To LastRow - 6) As String
|

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.