Ga naar hoofdinhoud

Hoe een draaitabel filteren op basis van een specifieke celwaarde in Excel?

Normaal gesproken filteren we gegevens in een draaitabel door items in de vervolgkeuzelijst te selecteren, zoals weergegeven in de onderstaande schermafbeelding. Eigenlijk kunt u een draaitabel filteren op basis van de waarde in een specifieke cel. De VBA-methode in dit artikel zal u helpen het probleem op te lossen.

Filter draaitabel op basis van een specifieke celwaarde met VBA-code


Filter draaitabel op basis van een specifieke celwaarde met VBA-code

De volgende VBA-code kan u helpen bij het filteren van een draaitabel op basis van een specifieke celwaarde in Excel. Ga als volgt te werk.

1. Voer een waarde in waarop u de draaitabel wilt filteren op basis van een cel van tevoren (hier selecteer ik cel H6).

2. Open het werkblad met de draaitabel die u op celwaarde filtert. Klik vervolgens met de rechtermuisknop op de bladtab en selecteer Bekijk code in het contextmenu. Zie screenshot:

3. In de opening Microsoft Visual Basic voor toepassingen -venster, kopieer onderstaande VBA-code naar het codevenster.

VBA-code: filter draaitabel op basis van celwaarde

Private Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice 20180702
    Dim xPTable As PivotTable
    Dim xPFile As PivotField
    Dim xStr As String
    On Error Resume Next
    If Intersect(Target, Range("H6:H7")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
    Set xPFile = xPTable.PivotFields("Category")
    xStr = Target.Text
    xPFile.ClearAllFilters
    xPFile.CurrentPage = xStr
    Application.ScreenUpdating = True
End Sub

Opmerkingen: In de code,

1) "Sheet1鈥滻s de naam van het werkblad.
2) "Draaitabel2鈥滻s de naam van de draaitabel.
3) Het filterveld in de draaitabel wordt 'Categorie".
4) De waarde waarop u de draaitabel wilt filteren, wordt in de cel geplaatst H6.
U kunt de bovenstaande variabelewaarden naar behoefte wijzigen.

4. druk de anders + Q toetsen om de Microsoft Visual Basic voor toepassingen venster.

Vervolgens filtert de draaitabel op basis van de waarde in cel H6, zoals onderstaand screenshot:

U kunt de celwaarde naar behoefte wijzigen in andere.

Note: Waarden die u in cel H6 typt, moeten exact overeenkomen met de waarden in de vervolgkeuzelijst Categorie van de draaitabel.


Gerelateerde artikelen:

Beste Office-productiviteitstools

馃 Kutools AI-assistent: Een revolutie teweegbrengen in de data-analyse op basis van: Intelligente uitvoering   |  Genereer code  |  Aangepaste formules maken  |  Analyseer gegevens en genereer grafieken  |  Roep Kutools-functies aan...
Populaire functies: Zoek, markeer of identificeer duplicaten   |  Verwijder lege rijen   |  Combineer kolommen of cellen zonder gegevens te verliezen   |   Ronde zonder formule ...
Super opzoeken: Meerdere criteria VLookup    VLookup met meerdere waarden  |   VOpzoeken over meerdere bladen   |   Fuzzy opzoeken ....
Geavanceerde vervolgkeuzelijst: Maak snel een vervolgkeuzelijst   |  Afhankelijke vervolgkeuzelijst   |  Multi-select vervolgkeuzelijst ....
Kolom Beheerder: Voeg een specifiek aantal kolommen toe  |  Kolommen verplaatsen  |  Schakel de zichtbaarheidsstatus van verborgen kolommen in  |  Vergelijk bereiken en kolommen ...
Uitgelichte functies: Raster focus   |  Ontwerpweergave   |   Grote formulebalk    Werkmap- en bladbeheer   |  resource Library (Auto-tekst)   |  Datumkiezer   |  Combineer werkbladen   |  Cellen coderen/decoderen    Stuur e-mails per lijst   |  Super filter   |   Speciaal filter (filter vet/cursief/doorhalen...) ...
Top 15 gereedschapsets12 Tekst Tools (toe te voegen tekst, Tekens verwijderen, ...)   |   50+ tabel Types (Gantt Chart, ...)   |   40+ Praktisch Formules (Bereken leeftijd op basis van verjaardag, ...)   |   19 Invoeging Tools (QR-code invoegen, Afbeelding invoegen vanaf pad, ...)   |   12 Camper ombouw Tools (Getallen naar woorden, Currency Conversion, ...)   |   7 Samenvoegen en splitsen Tools (Geavanceerd Combineer rijen, Gespleten cellen, ...)   |   ... en meer

Geef uw Excel-vaardigheden een boost met Kutools voor Excel en ervaar effici毛ntie als nooit tevoren. Kutools voor Excel biedt meer dan 300 geavanceerde functies om de productiviteit te verhogen en tijd te besparen.  Klik hier om de functie te krijgen die u het meest nodig heeft...

Omschrijving


Office-tabblad Brengt een interface met tabbladen naar Office en maakt uw werk veel gemakkelijker

  • Schakel bewerken en lezen met tabbladen in Word, Excel, PowerPoint in, Publisher, Access, Visio en Project.
  • Open en maak meerdere documenten in nieuwe tabbladen van hetzelfde venster in plaats van in nieuwe vensters.
  • Verhoogt uw productiviteit met 50% en vermindert honderden muisklikken voor u elke dag!
Comments (23)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Bonjour,

Merci beaucoup pour cette explication qui marche parfaitement.
En revanche, j'aimerais pouvoir utiliser ce code pour pouvoir filtrer deux tableaux crois茅s dynamiques en m锚me temps qui sont sur la m锚me feuille. La seule petite diff茅rence entre les deux, c'est qu'ils n'utilisent pas les m锚mes sources. En revanche, le filtre sur lequel se base ces TDC est le m锚me.

Pourriez-vous m'aider 脿 faire 茅voluer ce code afin que cela fonctionne ?

Voici le code utilis茅 quand il marche avec un TCD :

Private Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice 20180702
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("G4")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Cadrage").PivotTables("Tableau crois茅 dynamique7")
Set xPFile = xPTable.PivotFields("N掳PROJET")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub

Merci beaucoup
This comment was minimized by the moderator on the site
Hi Ambroise,

Sorry it's hard to amend this code to meet your needs. If you want to filter multiple pivot tables with a single filter, the methods in this article below may do you a favor:
How to connect a single slicer to multiple pivot tables in Excel?
This comment was minimized by the moderator on the site
Bonjour,
Merci beaucoup pour ces explications.

J'aimerai utiliser un filtre (1 cellule) en F4 par exemple qui filtrerait deux TCD qui sont sur la m锚me feuille.

Cela fonctionne tr猫s bien avec un TCD mais d猫s que j'essaye de combiner le second, 莽a ne marche pas.
Pourriez-vous m'aider ?

Merci beaucoup
Ambroise
This comment was minimized by the moderator on the site
How does it work with Power Pivot, when using multiple tables? I recorded macro changing the value in filter. Made few changes to make the above code work. But it throws Type mismatch error. No matter what I do.
This comment was minimized by the moderator on the site
Hi DK,
The method does not work for Power Pivot. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
Thank you so much for this code! I got it working after adjusting to meet my fields, but after formatting some changes on my sheet now it doesn't work! I moved it from A1 to B1, changed some cell formatting to make it stand out, etc. Nothing too crazy but now it doesn't update when I change text in B1. Anyone have any ideas?

Private Sub Worksheet_Change(ByVal Target As Range)
'test
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String

Dim x2PTable As PivotTable
Dim x2PFile As PivotField
Dim x2Str As String

Dim x3PTable As PivotTable
Dim x3PFile As PivotField
Dim x3Str As String

On Error Resume Next
If Intersect(Target, Range("b1")) Is Nothing Then Exit Sub

Application.ScreenUpdating = False

'tbl-1
Set xPTable = Worksheets("Line Report").PivotTables("PivotTable7")
Set xPFile = xPTable.PivotFields("Utopia Source")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr

'tbl-2
Set x2PTable = Worksheets("Line Report").PivotTables("PivotTable2")
Set x2PFile = x2PTable.PivotFields("Utopia Source")
x2Str = Target.Text
x2PFile.ClearAllFilters
x2PFile.CurrentPage = x2Str

'tbl-3
Set x3PTable = Worksheets("Line Report").PivotTables("PivotTable3")
Set x3PFile = x3PTable.PivotFields("Utopia Source")
x3Str = Target.Text
x3PFile.ClearAllFilters
x3PFile.CurrentPage = x3Str

Application.ScreenUpdating = True

End Sub
This comment was minimized by the moderator on the site
Hi Lance,
I tested your code and it works fine in my case. Changing the cell format does not affect the operation of the code.
This comment was minimized by the moderator on the site
Hello!

I am new with VBA and I would like to have a code to select a pivot filter based on a cell range.
How can I change "CurrentPage" to be a range value ?
Thank you!!
-------------------------------------------------------------------------------------------
Sub PrintTour()

ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"[Bereich 1].[Tour].[Tour ]"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"[Bereich 1].[Tour].[Tour]"). _
CurrentPage = "[Bereich 1].[Tour lt. Anlieferungstag].&[4001-01]"
End Sub
This comment was minimized by the moderator on the site
What if i want to link the selection cell to a different tab? This is my code so far
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xPTable1 As PivotTable
Dim xPFile1 As PivotField
Dim xStr1 As String
On Error Resume Next
If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable1 = Worksheets("SM_SKU PIVOTS").PivotTables("PivotTable1")
Set xPFile1 = xPTable1.PivotFields("Geography")
xStr1 = Target.Text
xPFile1.ClearAllFilters
xPFile1.CurrentPage = xStr1
Application.ScreenUpdating = True

Dim xPTable2 As PivotTable
Dim xPFile2 As PivotField
Dim xStr2 As String
On Error Resume Next
If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable2 = Worksheets("SM_SKU PIVOTS").PivotTables("PivotTable4")
Set xPFile2 = xPTable2.PivotFields("Geography")
xStr2 = Target.Text
xPFile2.ClearAllFilters
xPFile2.CurrentPage = xStr2
Application.ScreenUpdating = True

Dim xPTable3 As PivotTable
Dim xPFile3 As PivotField
Dim xStr3 As String
On Error Resume Next
If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable3 = Worksheets("SM_SKU PIVOTS").PivotTables("PivotTable8")
Set xPFile3 = xPTable3.PivotFields("Geography")
xStr3 = Target.Text
xPFile3.ClearAllFilters
xPFile3.CurrentPage = xStr3
Application.ScreenUpdating = True

End Sub
This comment was minimized by the moderator on the site
Ol谩, gostaria de saber se quisesse filtrar mais de uma categoria como poderia ser?
This comment was minimized by the moderator on the site
I would like to use multiple Worksheet Change code in same worksheet. How to do that? My code is as below:
Private Sub Worksheet_Change(ByVal Target As Range)
'Pivot table filter based on cell value
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("D20:D21")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
Set xPFile = xPTable.PivotFields("Designation")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_Change2(ByVal Target As Range)
'Pivot table filter based on cell value 2
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String
On Error Resume Next
If Intersect(Target, Range("H20:H21")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
Set xPFile = xPTable.PivotFields("Offering")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Is this possible with google sheets? If so, how?
This comment was minimized by the moderator on the site
Google Sheets will not require any pivot table. you can directly perform through Filter Function
This comment was minimized by the moderator on the site
I was able to modify/filter 3 different pivots which are on the same tab. I also added a row in my data set "No Data Found", otherwise it left the filter to "ALL" which I didn't want. The above was a great help to earn me Kudos with management so I wanted to share. Note the (All) is case sensitive took me a bit to figure that out.
Private Sub Worksheet_Change(ByVal Target As Range)
'test
Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String

Dim x2PTable As PivotTable
Dim x2PFile As PivotField
Dim x2Str As String

Dim x3PTable As PivotTable
Dim x3PFile As PivotField
Dim x3Str As String

On Error Resume Next
If Intersect(Target, Range("a2:e2")) Is Nothing Then Exit Sub

Application.ScreenUpdating = False

'tbl-1
Set xPTable = Worksheets("Graphical").PivotTables("PivotTable1")
Set xPFile = xPTable.PivotFields("MR Department - Department")
xStr = Target.Text
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
If xPFile.CurrentPage = "(All)" Then xPFile.CurrentPage = "No Data Found"

'tbl-2
Set x2PTable = Worksheets("Graphical").PivotTables("PivotTable2")
Set x2PFile = x2PTable.PivotFields("MR Department - Department")
x2Str = Target.Text
x2PFile.ClearAllFilters
x2PFile.CurrentPage = x2Str
If x2PFile.CurrentPage = "(All)" Then x2PFile.CurrentPage = "No Data Found"

'tbl-3
Set x3PTable = Worksheets("Graphical").PivotTables("PivotTable3")
Set x3PFile = x3PTable.PivotFields("MR Department - Department")
x3Str = Target.Text
x3PFile.ClearAllFilters
x3PFile.CurrentPage = x3Str
If x3PFile.CurrentPage = "(All)" Then x3PFile.CurrentPage = "No Data Found"

Application.ScreenUpdating = True

End Sub
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations