VBA to filter pivot column based off values from textbox

darthbane

New Member
Joined
Dec 13, 2017
Messages
28
Hello,
I would like some help with creating a macro that will filter one pivot field (column B) from my pivot table based on what the users enter in cell A4. The user can enter multiple values separated by commas to only show the rows they want to see. As the data will be 1000 rows - i would like a quick way to have it filtered down to just the specific rows that of are interest.

for example :
project number$ (MM)time (days)
aa-11030
aa-2580
bb-1790
bb-28240

<tbody>
</tbody>


The user can input aa-1,bb-1 into cell A4, to have the pivot filtered to show just those two rows

thanks in advance!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi darthbane,

Try the following code:

Code:
Sub PivotFilter()
    Dim pvtTable            As PivotTable
    Dim pvtItem             As PivotItem
    Dim varFilters          As Variant


    With ActiveSheet
        Set pvtTable = .PivotTables("[COLOR=#ff0000]PivotTable1[/COLOR]")
        varFilters = Split(.Range("A4").Value, ",")
    End With
    
    pvtTable.ClearAllFilters
    
    With pvtTable.PivotFields("project number")
        For Each pvtItem In .PivotItems
            pvtItem.Visible = IsNumeric(Application.Match(pvtItem.Value, varFilters, 0))
        Next pvtItem
    End With
End Sub

Change the name of your Pivot Table (red font) if necessary. Let me know if this solution works for you.
 
Upvote 0
Hi JustynaMK,

yes! this does work. However it is very slow. The VBA takes a good 6-7 minutes to run. Is there any way this can be done quicker? I was hoping for it to be almost instant.

Thank you for the help!!
 
Upvote 0
Glad it works!

Hopefully it's just a matter of disabling Calculations... please try the following code:

Code:
Sub PivotFilter()
    Dim pvtTable            As PivotTable
    Dim pvtItem             As PivotItem
    Dim varFilters          As Variant


    Application.Calculation = xlCalculationManual


    With ActiveSheet
        Set pvtTable = .PivotTables("PivotTable1")
        varFilters = Split(.Range("A4").Value, ",")
    End With
    
    pvtTable.ClearAllFilters
    
    With pvtTable.PivotFields("project number")
        For Each pvtItem In .PivotItems
            pvtItem.Visible = IsNumeric(Application.Match(pvtItem.Value, varFilters, 0))
        Next pvtItem
    End With
    
    Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Hi darthbane,

Unfortunately I couldn't recreate your issue on my side. I created over 1000 rows of exemplary data for my Pivot Table, with 1000 different project numbers, and it still fully processes the code under 10 seconds. Is there any chance you could share the file so that I can check what is happening?
 
Upvote 0
Hi darthbane,

Unfortunately I couldn't recreate your issue on my side. I created over 1000 rows of exemplary data for my Pivot Table, with 1000 different project numbers, and it still fully processes the code under 10 seconds. Is there any chance you could share the file so that I can check what is happening?



Hi,

I am not able to see a way to add attachements on this forum. Here is a test copy uploaded to google drive.
https://drive.google.com/file/d/1vUiER-ct478wvB3wA-22pYn3fo-1m0Qy/view?usp=sharing

Thanks again, I appreciate you looking into this! :)
 
Upvote 0
Hi darthbane,

Thanks for sharing the file. I just run your code on my side ("Sheet1") and it took me exactly 15 seconds to fully process it. Did you attach the whole (complete) file?

Alternatively, you can add "Debug.Print Time" to your code and see which part takes the longest to run. You need to have VBA Immediate Window (Alt+F11 -> Ctrl+G) open in order to see the results.

Sorry, cannot think of any other solution!

Code:
Sub PivotFilter()
    Dim pvtTable            As PivotTable
    Dim pvtItem             As PivotItem
    Dim varFilters          As Variant
    
[COLOR=#ff0000]    Debug.Print Time[/COLOR]


    Application.Calculation = xlCalculationManual


    With ActiveSheet
        Set pvtTable = .PivotTables("PivotTable25")
        varFilters = Split(.Range("A1").Value, ",")
    End With
    
    pvtTable.ClearAllFilters
    
[COLOR=#ff0000]    Debug.Print Time[/COLOR]
    
    With pvtTable.PivotFields("Plan ID")
        For Each pvtItem In .PivotItems
            pvtItem.Visible = IsNumeric(Application.Match(pvtItem.Value, varFilters, 0))
        Next pvtItem
    End With
    
    Application.Calculation = xlCalculationAutomatic
    
[COLOR=#ff0000]    Debug.Print Time[/COLOR]
End Sub
 
Upvote 0
Thanks JustynaMK.
I tried running the VBA code again and it worked within 15 seconds!! Not sure why it was taking soo long before it may have been due to having a large amount of excel worksheets open at once..
Anyways you are awesome! thanks again :)
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,897
Members
449,097
Latest member
dbomb1414

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