0

我是 VBA 新手,正在尝试编写一个创建数据透视表的宏。我需要过滤各种字段,并尝试使用 PivotFilters.Add 和 PivotItems 只让某些事情通过......有时它可以工作,但有时它会引发错误。以下代码可以正常工作:

Sub CreatePivot()
Dim objTable As PivotTable, objField As PivotField

ActiveWorkbook.Sheets("CP Monthly Data").Select
Range("A1").Select
Set objTable = Sheet1.PivotTableWizard
objTable.Name = "Resource Requests"
objTable.InGridDropZones = True
objTable.RowAxisLayout xlTabularRow

Set objField = objTable.PivotFields("Company name")
objField.Orientation = xlRowField
objField.Position = 1

Set objField = objTable.PivotFields("Probability Status")
objField.Orientation = xlRowField
objField.Position = 2
objField.PivotItems("X - Lost - 0%").Visible = False
objField.PivotItems("X - On Hold - 0%").Visible = False
objField.AutoSort xlDescending, "Probability Status"

Set objField = objTable.PivotFields("Project")
objField.Orientation = xlRowField
objField.Position = 3

Set objField = objTable.PivotFields("Project manager")
objField.Orientation = xlRowField
objField.Position = 4

Set objField = objTable.PivotFields("Resource name")
objField.Orientation = xlRowField
objField.Position = 5
objField.AutoSort xlAscending, "Resource name"

Set objField = objTable.PivotFields("June, 2012")
objField.Orientation = xlDataField
objField.Function = xlSum
objField.NumberFormat = "##"
objField.Caption = "June"

Set objField = objTable.PivotFields("Workgroup Name")
objField.Orientation = xlPageField
objField.PivotItems("ATG").Visible = False
objField.PivotItems("India - ATG").Visible = False
objField.PivotItems("India - Managed Middleware").Visible = False

Application.DisplayAlerts = True
End Sub

“资源名称”字段给我带来了问题。我只需要显示以“*TBD”开头的资源名称,并排除名称中包含“ATG”的资源名称。到目前为止,我已经尝试了以下方法:

Set objField = objTable.PivotFields("Resource name")
objField.Orientation = xlRowField
objField.Position = 5
objField.PivotFilters.Add xlCaptionContains, Value1:="TBD"
objField.PivotFilters.Add xlCaptionDoesNotContain, Value1:="ATG"
objField.AutoSort xlAscending, "Resource name"

返回“运行时错误'1004':应用程序定义或对象定义的错误

这不正是我所需要的,因为我还需要过滤掉名称中没有“TBD”的那些,但我也尝试过:

Set objField = objTable.PivotFields("Resource name")
objField.Orientation = xlRowField
objField.Position = 5
objField.PivotItems("*ATG*").Visible = False
objField.AutoSort xlAscending, "Resource name"

返回“运行时错误'1004':无法获取 PivotField 类的 PivotItems 属性

我还尝试录制宏并根据我的代码检查结果。结果使用我尝试过的 PivotFilters.Add 。录制的宏和我的代码之间的主要区别在于 PivotTableWizard 的使用,我开始怀疑这是否重要……我是新手,记得吗?

有想法该怎么解决这个吗?我正在使用 Excel 2010 并且已经花费了数小时来搜索它,但我尝试过的任何方法都没有奏效。提前感谢您的帮助!!

4

1 回答 1

1

不幸的是,您不能将相同类型的过滤器应用于 PT 中的一个字段。这个链接是一个不错的阅读。

要解决您的问题,您可以在上面的帖子中替换此代码块:

Set objField = objTable.PivotFields("Resource name")
objField.Orientation = xlRowField
objField.Position = 5
objField.AutoSort xlAscending, "Resource name"

使用以下代码:

'1) Filter on any thing that contains `TBD`
Set objField = objTable.PivotFields("Resource name")
objField.Orientation = xlRowField
objField.Position = 5
objField.PivotFilters.Add xlCaptionContains, Value1:="TBD"

'2) Loop through the items in the field and uncheck anything with `ATG` in the name
Dim pi As PivotItem
Dim i as Integer

For i = 1 To objField.PivotItems.Count
    If InStr(1, objField.PivotItems(i), "ATG") <> 0 Then
        objField.PivotItems(i).Visible = False
    End If
Next

objField.AutoSort xlAscending, "Resource name"
于 2012-06-22T20:01:19.437 回答