Բաց թողնել հիմնական բովանդակությունը

Ինչպե՞ս զտել առանցքի աղյուսակը ՝ հիմնված Excel- ում բջջի որոշակի արժեքի վրա:

Սովորաբար, մենք զտում ենք տվյալները առանցքային աղյուսակում՝ ընտրելով տարրեր բացվող ցանկում, ինչպես ցույց է տրված ստորև ներկայացված սքրինշոթում: Փաստորեն, դուք կարող եք զտել առանցքային աղյուսակը՝ հիմնվելով որոշակի բջիջի արժեքի վրա: Այս հոդվածում VBA մեթոդը կօգնի ձեզ լուծել խնդիրը:

Terտեք առանցքային աղյուսակը `հիմնվելով VBA կոդով հատուկ բջջային արժեքի վրա


Terտեք առանցքային աղյուսակը `հիմնվելով VBA կոդով հատուկ բջջային արժեքի վրա

Հետևյալ VBA կոդը կարող է օգնել զտել առանցքային աղյուսակը, որը հիմնված է Excel- ում բջջի որոշակի արժեքի վրա: Խնդրում եմ, արեք հետևյալ կերպ.

1. Խնդրում ենք մուտքագրել մի արժեք, որի վրա դուք զտեք առանցքային աղյուսակը ՝ հիմնվելով վանդակի վրա (այստեղ ես ընտրում եմ H6 բջիջ):

2. Բացեք աշխատանքային թերթը պարունակում է առանցքային աղյուսակ, որը դուք կզտեք ըստ բջջային արժեքի: Դրանից հետո աջ կտտացրեք թերթիկի ներդիրին և համատեքստի ընտրացանկից ընտրեք Դիտել կոդը: Տեսեք,

3. Բացման մեջ Microsoft Visual Basic հավելվածների համար պատուհանը, պատճենեք ներքևում գտնվող VBA կոդն օրենսգրքի պատուհանում:

VBA կոդ. Filտել առանցքային աղյուսակը `հիմնված բջջային արժեքի վրա

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

NotesԿոդում,

1) "Sheet1”- աշխատանքային էջի անվանումն է:
2) "Առանցքային աղյուսակ 2”- առանցքային աղյուսակի անվանումն է:
3) առանցքային աղյուսակում զտիչ դաշտը կոչվում է. "կատեգորիա".
4) Արժեքը, որը ցանկանում եք զտել առանցքային աղյուսակը, տեղադրվում է վանդակում H6.
Վերը նշված փոփոխական արժեքները կարող եք փոխել ըստ անհրաժեշտության:

4. Սեղմեք ալտ + Q ստեղները փակելու համար Microsoft Visual Basic հավելվածների համար պատուհան.

Այնուհետև առանցքային աղյուսակը զտվում է ՝ հիմնվելով H6 բջիջի արժեքի վրա, ինչպես ցույց է տրված ստորև նշված էկրանի նկարը.

Բջջի արժեքը կարող եք փոխել ուրիշի ՝ ըստ ձեզ անհրաժեշտի:

Նշում. H6 բջիջում ձեր մուտքագրած արժեքները պետք է ճշգրտորեն համապատասխանեն առանցքային աղյուսակի Կատեգորիայի բացվող ցուցակի արժեքներին:


Առնչվող հոդվածներ քանակը:

Գրասենյակի արտադրողականության լավագույն գործիքները

🤖 Kutools AI օգնականՀեղափոխություն կատարել տվյալների վերլուծության հիման վրա՝ Խելացի կատարում   |  Ստեղծեք ծածկագիր  |  Ստեղծեք հատուկ բանաձևեր  |  Վերլուծել տվյալները և ստեղծել գծապատկերներ  |  Invoke Kutools-ի գործառույթները...
Հանրաճանաչ հատկություններ: Գտեք, ընդգծեք կամ նույնականացրեք կրկնօրինակները   |  Deleteնջել դատարկ շարքերը   |  Միավորել սյունակները կամ բջիջները՝ առանց տվյալների կորստի   |   Կլոր առանց բանաձևի ...
Super Փնտրել: Բազմաթիվ չափանիշների VLookup    Բազմակի արժեք VLookup  |   VLookup բազմաթիվ թերթերում   |   Fuzzy Փնտրել ....
Ընդլայնված բացվող ցուցակ: Արագ ստեղծեք բացվող ցուցակը   |  Կախված բացվող ցուցակ   |  Բազմակի ընտրություն Drop Down ցուցակ ....
Սյունակի կառավարիչ: Ավելացրեք որոշակի քանակությամբ սյունակներ  |  Տեղափոխել սյունակները  |  Փոխարկել թաքնված սյունակների տեսանելիության կարգավիճակը  |  Համեմատեք միջակայքերը և սյունակները ...
Առանձնահատկություններ: Ցանցի կենտրոնացում   |  Դիզայնի տեսք   |   Մեծ Formula Bar    Աշխատանքային գրքույկի և թերթիկների կառավարիչ   |  Ռեսուրսների գրադարան (Ավտոմատ տեքստ)   |  Ամսաթիվ ընտրող   |  Միավորել աշխատանքային թերթերը   |  Գաղտնագրել/գաղտնազերծել բջիջները    Ուղարկեք նամակներ ըստ ցանկի   |  Սուպեր զտիչ   |   Հատուկ զտիչ (զտել թավ/շեղ/շեղված...) ...
Լավագույն 15 գործիքների հավաքածու12 Տեքստ Գործիքներ (Ավելացրեք տեքստ, Հեռացնել նիշերը, ...)   |   50+ Աղյուսակ Տեսակներ (Գանտի աղյուսակը, ...)   |   40+ Գործնական Բանաձեւեր (Հաշվարկել տարիքը ՝ ելնելով ծննդյան տարեդարձից, ...)   |   19 միացում Գործիքներ (Տեղադրեք QR կոդ, Տեղադրեք նկար ուղուց, ...)   |   12 Փոխարկում Գործիքներ (Բառեր համարներ, Արտարժույթի փոխակերպումը, ...)   |   7 Միաձուլում և պառակտում Գործիքներ (Ընդլայնված կոմբինատ տողեր, Պառակտված բջիջներ, ...)   |   ... եւ ավելին

Լրացրեք ձեր Excel-ի հմտությունները Kutools-ի հետ Excel-ի համար և փորձեք արդյունավետությունը, ինչպես երբեք: Kutools-ը Excel-ի համար առաջարկում է ավելի քան 300 առաջադեմ առանձնահատկություններ՝ արտադրողականությունը բարձրացնելու և ժամանակ խնայելու համար:  Սեղմեք այստեղ՝ Ձեզ ամենաշատ անհրաժեշտ հատկանիշը ստանալու համար...

Նկարագրություն


Office Tab- ը Tabbed ինտերֆեյսը բերում է Office, և ձեր աշխատանքը շատ ավելի դյուրին դարձրեք

  • Միացնել ներդիրներով խմբագրումը և ընթերցումը Word, Excel, PowerPoint- ով, Հրատարակիչ, Access, Visio և Project:
  • Բացեք և ստեղծեք բազմաթիվ փաստաթղթեր նույն պատուհանի նոր ներդիրներում, այլ ոչ թե նոր պատուհաններում:
  • Բարձրացնում է ձեր արտադրողականությունը 50%-ով և նվազեցնում մկնիկի հարյուրավոր սեղմումները ձեզ համար ամեն օր:
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