简体   繁体   中英

changing pivot table Report Filter with VBA

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.

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

您需要将Range(“ SelectedProj”)格式设置为字符串,或者只需在输入数字之前简单添加即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM