changing pivot table Report Filter with VBA

依然范特西╮ 提交于 2019-12-13 07:01:18

问题


I have a VBA code that changes the a pivot table Report Filter based on a cell.

It all works fine with inputs that are alphanumeric. When the input is a number, I get an error.

Here is the code:

Sub ProjSelect_PivotsUpdate()
    Dim Selected_Proj

    Selected_Proj = Worksheets("Parameters").Range("SelectedProj")
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Project").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Project").CurrentPage = _
    Selected_Proj
End Sub

Here is the error code:

Run-time error '1004':

Application-defined or object-defined error.


回答1:


trimming the value would solve your problem.

Sub ProjSelect_PivotsUpdate()
    Dim Selected_Proj

    Selected_Proj = Worksheets("Parameters").Range("SelectedProj")
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Project").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Project").CurrentPage =  Trim(Selected_Proj)
End Sub



回答2:


You need to set the Range("SelectedProj") format to string,or just simple add ' before you input a number.



来源:https://stackoverflow.com/questions/18977724/changing-pivot-table-report-filter-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!