Vba ကို အသုံးပြု၍ pivottable များကို စစ်ထုတ်နည်း (ဥပမာများဖြင့်)
VBA ကို အသုံးပြု၍ Excel ရှိ pivot ဇယားများကို စစ်ထုတ်ရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်သည်-
နည်းလမ်း 1- တန်ဖိုးတစ်ခုအပေါ် အခြေခံ၍ ဆုံချက်ဇယားကို စစ်ထုတ်ပါ။
SubFilterPivotTable ()
Dim pf As PivotField
Dim myFilter As String
Set pf = ActiveSheet.PivotTables(" PivotTable1 ").PivotFields(" Position ")
myFilter = ActiveWorkbook.Sheets(" Sheet1 ").Range(" J2 ").Value
pf.PivotFilters.Add2 xlCaptionEquals, , myFilter
End Sub
ဤ macro သည် PivotTable1 ဟုခေါ်သော ဆုံချက်ဇယားကို စစ်ထုတ်မည်ဖြစ်ပြီး ဆုံချက်ဇယား၏ Position ကော်လံရှိ တန်ဖိုးသည် Sheet1 ၏ဆဲလ် J2 ၏တန်ဖိုးနှင့်ညီမျှသည့် အတန်းများကိုသာပြသမည်ဖြစ်သည်။
နည်းလမ်း 2- တန်ဖိုးများစွာကို အခြေခံ၍ PivotTable ကို စစ်ထုတ်ပါ။
Sub FilterPivotTableMultiple()
Dim v Ace Variant
Dim i As Integer, j As Integer
Dim pf As PivotField
Set pf = ActiveSheet.PivotTables(" PivotTable1 ").PivotFields(" Position ")
'specify range with values to filter on
v = Range(" J2:J3 ")
'clear existing filters
pf.ClearAllFilters
'apply filter to pivot table
With pf
For i = 1 TB pf.PivotItems.Count
j = 1
Do While j <= UBound(v, 1) - LBound(v, 1) + 1
If pf.PivotItems(i).Name = v(j, 1) Then
pf.PivotItems(pf.PivotItems(i).Name).Visible = True
Exit Do
Else
pf.PivotItems(pf.PivotItems(i).Name).Visible = False
End If
j = j + 1
Loop
Next i
End With
End Sub
ဤထူးခြားသော macro သည် PivotTable1 ဟုခေါ်သော ဆုံချက်ဇယားကို စစ်ထုတ်မည်ဖြစ်ပြီး pivot table ၏ Position ကော်လံရှိ တန်ဖိုးသည် ဆဲလ်အကွာအဝေး J2:J3 ရှိ တန်ဖိုးများထဲမှ တစ်ခုနှင့် ညီမျှသည့် အတန်းများကိုသာ ပြသမည်ဖြစ်သည်။
နည်းလမ်း 3- PivotTable စစ်ထုတ်မှုများကို ဖယ်ရှားပါ။
SubClearPivotTableFilter ()
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables(" PivotTable1 ")
pt.ClearAllFilters
End Sub
ဤအထူးမက်ခရိုသည် PivotTable1 ဟုခေါ်သော ဆုံချက်ဇယားမှ စစ်ထုတ်မှုများအားလုံးကို ရှင်းလင်းပါမည်။
အောက်ဖော်ပြပါ ဥပမာများသည် ဤနည်းလမ်းတစ်ခုစီကို လက်တွေ့အသုံးချနည်းကို ပြသထားသည်။
ဥပမာ 1- တန်ဖိုးတစ်ခုအပေါ် အခြေခံ၍ pivot table ကို စစ်ထုတ်ပါ။
မတူညီသောအသင်းများနှင့် ရာထူးများမှ ဘတ်စကက်ဘောကစားသမားများမှ ရမှတ်များကို အကျဉ်းချုပ်ဖော်ပြရန် Excel ရှိ ဒေတာအစုတစ်ခုမှ ဆုံချက်ဇယားတစ်ခုကို ဖန်တီးခဲ့သည်ဆိုကြပါစို့။
Position ကော်လံရှိ တန်ဖိုးသည် Guard ဖြစ်သည့် အတန်းများကိုသာ ပြသရန် ဆုံချက်ဇယားကို စစ်ထုတ်လိုသည်ဆိုပါစို့။
ဒါကိုလုပ်ဖို့ အောက်ပါ macro ကို ဖန်တီးနိုင်ပါတယ်။
SubFilterPivotTable ()
Dim pf As PivotField
Dim myFilter As String
Set pf = ActiveSheet.PivotTables(" PivotTable1 ").PivotFields(" Position ")
myFilter = ActiveWorkbook.Sheets(" Sheet1 ").Range(" J2 ").Value
pf.PivotFilters.Add2 xlCaptionEquals, , myFilter
End Sub
ကျွန်ုပ်တို့ ဤမက်ခရိုကို လုပ်ဆောင်သောအခါ၊ ရာထူးကော်လံတွင် တန်ဖိုးသည် Guard ဖြစ်သည်- အတန်းများကိုသာပြသရန် ဆုံချက်ဇယားကို အလိုအလျောက် စစ်ထုတ်ပါသည်။
ရာထူးကော်လံရှိ တန်ဖိုးသည် Guard အတန်းများကိုသာပြသရန် ဆုံချက်ဇယားကို စစ်ထုတ်ထားပါသည်။
ဥပမာ 2- တန်ဖိုးများစွာကို အခြေခံ၍ pivot ဇယားကို စစ်ထုတ်ပါ။
ရာထူးကော်လံရှိ Guard သို့မဟုတ် Center တွင် တန်ဖိုးရှိသောအတန်းများကိုသာပြသရန် pivot ဇယားကို စစ်ထုတ်လိုသည်ဆိုပါစို့။
ဒါကိုလုပ်ဖို့ အောက်ပါ macro ကို ဖန်တီးနိုင်ပါတယ်။
Sub FilterPivotTableMultiple()
Dim v Ace Variant
Dim i As Integer, j As Integer
Dim pf As PivotField
Set pf = ActiveSheet.PivotTables(" PivotTable1 ").PivotFields(" Position ")
'specify range with values to filter on
v = Range(" J2:J3 ")
'clear existing filters
pf.ClearAllFilters
'apply filter to pivot table
With pf
For i = 1 TB pf.PivotItems.Count
j = 1
Do While j <= UBound(v, 1) - LBound(v, 1) + 1
If pf.PivotItems(i).Name = v(j, 1) Then
pf.PivotItems(pf.PivotItems(i).Name).Visible = True
Exit Do
Else
pf.PivotItems(pf.PivotItems(i).Name).Visible = False
End If
j = j + 1
Loop
Next i
End With
End Sub
ကျွန်ုပ်တို့ ဤမက်ခရိုကို လုပ်ဆောင်သောအခါ၊ ရာထူးကော်လံတွင် တန်ဖိုးသည် Guard သို့မဟုတ် Center ဖြစ်သည့် အတန်းများကိုသာ ပြသရန် ဆုံချက်ဇယားကို အလိုအလျောက် စစ်ထုတ်ပါသည်။
ရာထူးကော်လံရှိ တန်ဖိုးသည် Guard သို့မဟုတ် Center အတန်းများကိုသာပြသရန် ဆုံချက်ဇယားကို စစ်ထုတ်ထားပါသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် VBA တွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
VBA- pivot ဇယားများကို ပြန်လည်ဆန်းသစ်နည်း
VBA- ထပ်နေသောတန်ဖိုးများကို ဖယ်ရှားနည်း
VBA- အပိုင်းအခြားရှိ အတန်းအရေအတွက်ကို ရေတွက်နည်း