Can anyone help me ?
I got pivot table and I wanna autofilter with the criteria of 1
However , after running the macro, it didnt show the updated table
Private Sub Simpat2_CreatePivot()
Dim ws As Worksheet 'Add new ws pivot
Dim pc As PivotCache
Dim pt As PivotTable
Dim objField As PivotField
Dim rngData As Range
Dim wsData As Worksheet 'Old ws
Dim dataField As PivotField
Dim lastRow As Long
Dim lastColumn As Long
Dim MaxRowNum As Long
'Step 15 - 17
'Select the sheet and first cell of the table that contains the data.
'Set worksheet which contain the source data
Set wsData = Worksheets("Simpat")
'Delete any prior pivot tables
' For Each pt In wsData.PivotTables
' pt.TableRange2.Clear
' Next pt
'determine source data range (dynamic):
'last row in column no. 1:
lastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row
'last column in row no. 1:
lastColumn = wsData.Cells(1, Columns.Count).End(xlToLeft).Column
'Data Range
Set rngData = wsData.Cells(1, 1).Resize(lastRow, lastColumn)
'Create Pivot Caches
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:= _
xlPivotTableVersion15)
'Creating Pivot Table
Sheets("SimPat").Select
Set pt = pc.CreatePivotTable(TableDestination:="", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion15)
'Turn off automatic updation of Pivot Table during the process of its creation to speed up code.
pt.ManualUpdate = True
'Setting fields
Set objField = pt.PivotFields("Last Name")
objField.Orientation = xlRowField
objField.Position = 1
'Set Data Field
With pt.PivotFields("Last Name")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
End With
'Count Max Row
MaxRowNum = 1
Do While Cells(MaxRowNum, 2) <> "" Or Cells(MaxRowNum + 1, 2) <> ""
MaxRowNum = MaxRowNum + 1
Loop
'Autofilter
Range("B2").Select
Selection.AutoFilter
ActiveSheet.Range("A2:B" & MaxRowNum).AutoFilter Field:=2, Criteria1:="1"
ActiveWindow.SmallScroll Down:=-15
Range("C2").Select
'Turn on automatic update / calculation in the Pivot Table
pt.ManualUpdate = False
End Sub
Bookmarks