Ana içeriğe atla

Excel'deki belirli bir hücre değerine göre Pivot tablo nasıl filtrelenir?

Normalde, aşağıdaki ekran görüntüsünde gösterildiği gibi açılır listedeki öğeleri seçerek bir Özet Tablodaki verileri filtreliyoruz. Aslında, belirli bir hücredeki değere göre bir pivot tabloya filtre uygulayabilirsiniz. Bu makaledeki VBA yöntemi, sorunu çözmenize yardımcı olacaktır.

VBA kodu ile belirli bir hücre değerine göre Pivot Tabloyu filtreleyin


VBA kodu ile belirli bir hücre değerine göre Pivot Tabloyu filtreleyin

Aşağıdaki VBA kodu, Excel'deki belirli bir hücre değerine göre bir Pivot Tabloyu filtrelemenize yardımcı olabilir. Lütfen aşağıdaki işlemleri yapın.

1. Lütfen Pivot Tabloyu önceden bir hücreye göre filtreleyeceğiniz bir değer girin (burada H6 hücresini seçiyorum).

2. Hücre değerine göre filtreleyeceğiniz Pivot Tabloyu içeren çalışma sayfasını açın. Ardından, sayfa sekmesine sağ tıklayın ve içerik menüsünden Kodu Görüntüle'yi seçin. Ekran görüntüsüne bakın:

3. Açılışta Uygulamalar için Microsoft Visual Basic penceresinde, VBA kodunu Kod penceresine kopyalayın.

VBA kodu: Pivot Tablosunu hücre değerine göre filtrele

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

notlar: Kodda,

1) "Sheet1"Çalışma sayfasının adıdır.
2) "Özet Tablo2"Pivot Tablonun adıdır.
3) Pivot tablodaki filtreleme alanına "Kategoriler".
4) Pivot tabloyu filtrelemek istediğiniz değer hücreye yerleştirilir H6.
Yukarıdaki değişken değerlerini ihtiyaç duyduğunuz şekilde değiştirebilirsiniz.

4. Tuşuna basın. Ara Toplam + Q kapatmak için anahtarlar Uygulamalar için Microsoft Visual Basic pencere.

Ardından Pivot Tablo, aşağıda gösterilen ekran görüntüsü gibi H6 hücresindeki değere göre filtreleme yapıyor:

İhtiyaç duyduğunuzda hücre değerini başkalarına değiştirebilirsiniz.

not: H6 hücresine yazdığınız değerler, Pivot Tablosunun Kategori açılır listesindeki değerlerle tam olarak eşleşmelidir.


İlgili yazılar:

En İyi Ofis Üretkenlik Araçları

🤖 Kutools AI Yardımcısı: Aşağıdakilere dayalı olarak veri analizinde devrim yaratın: Akıllı Yürütme   |  Kodunu oluşturun  |  Özel Formüller Oluşturun  |  Verileri Analiz Edin ve Grafikler Oluşturun  |  Kutools İşlevlerini Çağır...
Popüler Özellikler: Yinelenenleri Bul, Vurgula veya Tanımla   |  Boş Satırları Sil   |  Veri Kaybı Olmadan Sütunları veya Hücreleri Birleştirin   |   Formülsüz Tur ...
Süper Arama: Çoklu Ölçütlü VLookup    Çoklu Değer VLookup  |   Birden Çok Sayfada VLookup   |   Bulanık Arama ....
Gelişmiş Açılır Liste: Hızla Açılır Liste Oluşturun   |  Bağımlı Açılır Liste   |  Çoklu Seçim Açılır Liste ....
Sütun Yöneticisi: Belirli Sayıda Sütun Ekleme  |  Sütunları Taşı  |  Gizli Sütunların Görünürlük Durumunu Değiştir  |  Aralıkları ve Sütunları Karşılaştırın ...
Öne Çıkan Özellikler: Izgara Odağı   |  Tasarım görünümü   |   Büyük Formül Çubuğu    Çalışma Kitabı ve Sayfa Yöneticisi   |  Kaynak Kütüphanesi (Otomatik metin)   |  Tarih Seçici   |  Çalışma Sayfalarını Birleştirin   |  Hücreleri Şifrele/Şifresini Çöz    E-postaları Listeye Göre Gönder   |  Süper Filtre   |   Özel Filtre (kalın/italik/üstü çizili filtre...) ...
En İyi 15 Araç Seti12 Metin Tools (Metin ekle, Karakterleri Kaldır, ...)   |   50+ Grafik Türleri (Gantt şeması, ...)   |   40+ Pratik Formüller (Yaşı doğum gününe göre hesapla, ...)   |   19 sokma Tools (QR Kodunu Girin, Yoldan Resim Ekle, ...)   |   12 Dönüştürme Tools (Sayılardan Kelimelere, Para Birimi Dönüştürme, ...)   |   7 Birleştir ve Böl Tools (Gelişmiş Kombine Satırları, Bölünmüş hücreler, ...)   |   ... ve dahası

Kutools for Excel ile Excel Becerilerinizi Güçlendirin ve Daha Önce Hiç Olmadığı Gibi Verimliliği Deneyimleyin. Kutools for Excel, Üretkenliği Artırmak ve Zamandan Tasarruf Etmek için 300'den Fazla Gelişmiş Özellik Sunar.  En Çok İhtiyacınız Olan Özelliği Almak İçin Buraya Tıklayın...

Açıklama


Office Tab, Office'e Sekmeli Arayüz Getirir ve İşinizi Çok Daha Kolay Hale Getirir

  • Word, Excel, PowerPoint'te sekmeli düzenlemeyi ve okumayı etkinleştirin, Publisher, Access, Visio ve Project.
  • Yeni pencereler yerine aynı pencerenin yeni sekmelerinde birden çok belge açın ve oluşturun.
  • Üretkenliğinizi% 50 artırır ve her gün sizin için yüzlerce fare tıklamasını azaltır!
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