使用 VBA 更改数据透视表报表过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18977724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:28:34  来源:igfitidea点击:

changing pivot table Report Filter with VBA

excelexcel-vbapivot-tablevba

提问by user2810405

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

我有一个基于单元格更改数据透视表报表过滤器的 VBA 代码。

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.

运行时错误“1004”:

应用程序定义或对象定义的错误。

回答by CompFreak

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

回答by Taotao

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

您需要将 Range("SelectedProj") 格式设置为字符串,或者在输入数字之前简单地添加 '。