Auto Filter Pivot Table

Im trying to autofilter a Excel Pivot Table.
If I run the code in a normal sheet , it works, but when I run the same code to filter a dynamic range I get an error.

I need to filter the column "D" with two criterias

This is the code I'm using:

Dim sw, dw As Object
Dim ss, ds As Object
Dim excel, sheet, varUsedRange As Object
Dim FilteredCount as Long


sw = GetWorkbook(handle, workbookname)
ss = GetWorksheet(handle, workbookname, worksheetname)

excel = ss.Application
sheet = excel.ActiveSheet

varUsedRange = sheet.UsedRange().address

If sheet.AutoFilterMode Then
sheet.AutoFilterMode = False
End If

sheet.range(varUsedRange ).AutoFilter (Field:=4, Criteria1:="=611")

FilteredCount = sheet.AutoFilter.Range.Columns("D").SpecialCells(12).Cells.Count - 1

If FilteredCount > 0 then

End if

Success = True

Catch e As Exception
Success = False
Message = e.Message
sw = Nothing
ss = Nothing
dw = Nothing
ds = Nothing
excel = Nothing
sheet = Nothing
varUsedRange = Nothing
End Try
Last edited: