Я пытаюсь отфильтровать сводный код, чтобы отображались только столбцы со значениями = 2.Как мне его кодировать?
Хотя я пробовал фильтровать вручную, выбрав ячейку, которая граничит с таблицей (но находится вне области сводки) и выбрав Фильтр на вкладке Данные.Однако в оперативном плане это не является устойчивым, и я хотел бы вместо этого создать код для автоматизации.
Sub getpivotUI2()
' getpivotUI2 Macro
' Create PivotTable from Task_Sheet to filter duplicate bill (UI2)
Dim P2Sheet As Worksheet, TSheet As Worksheet
Dim P2Cache As PivotCache
Dim P2Table As PivotTable
Dim P2Range As Range
Dim L2Row As Long, L2Col As Long
' Declaring the variables above
Set TSheet = ThisWorkbook.Worksheets("Task_Sheet")
Set P2Sheet = ThisWorkbook.Worksheets("pivot_UI2")
With TSheet
L2Row = .Cells(.Rows.Count, 1).End(xlUp).Row
L2Col = .Cells(4, .Columns.Count).End(xlToLeft).Column
Set P2Range = .Range(.Cells(4, 1), .Cells(L2Row, L2Col)) ' set the data source of the Pivot-Cache
End With
'Macros above determine where the cursor is referenced
P2Sheet.Cells.Delete 'Removing all previous data the pivotTable worksheet
' set the Pivot-Cache object
Set P2Cache = ActiveWorkbook.PivotCaches.Add(xlDatabase, P2Range.Address(0, 0, xlA1, xlExternal))
' set the Pivot-Table object
Set P2Table = P2Sheet.PivotTables.Add(PivotCache:=P2Cache, TableDestination:=P2Sheet.Range("A3"), TableName:="PivotTableUI2")
'Macros above create a pivot cache and address to insert the new pivot table
' ~~~ For Debug Only ~~~
Dim PTFld As PivotField
For Each PTFld In P2Table.PivotFields
Debug.Print PTFld.Name
Next PTFld
With P2Table.PivotFields("UI2")
.Orientation = xlRowField
.Position = 1
End With
With P2Table.PivotFields("Count_UI2")
.Orientation = xlDataField
.Function = xlCount
.Name = "Count of UI2"
.Position = 1
End With
With P2Table.PivotFields("R Patient" & Chr(10) & "Count")
.Orientation = xlDataField
.Function = xlCount
.Name = "Count of R Patient"
.Position = 2
End With
With P2Table.PivotFields("PR Patient" & Chr(10) & "Count")
.Orientation = xlDataField
.Function = xlCount
.Name = "Count of PR Patient"
.Position = 3
End With
'Macros above inserts a row field and data field in the pivot table
With P2Table.PivotFields("data")
.Orientation = xlColumnField
End With
'Macros above is to arrange the data in the datafield in columns
End Sub
Sub filterUI2pivot()
Dim UI2PT As PivotTable
Set UI2PT = ActiveSheet.PivotTables("PivotTableUI2")
With UI2PT
.ClearAllFilters
.PivotFields("Count of UI2").PivotFilters.Add Type:=xlValueEquals, Value1:=2 ßRun-time error 1004 <> Application-defined or object-defined error
End With
End Sub
I expect an output where pivot table rows are strunk to show items that has a value of 2 in the datafield.
enter code here