为什么我的PivotFilters不工作?为什么我的宏只能在我的电脑上运行?

时间:2012-07-03 16:49:42

标签: excel-vba templates filter pivot-table vba

我正在使用Excel 2010并在VBA中编写宏。我有两个问题:1 - 我的大部分数据透视表实际上都没有过滤,2 - 我已经为一个启用Excel宏的模板制作了这个宏,它可以在我的机器上运行正常,但如果通过电子邮件发送模板并尝试运行另一台机器上的宏我收到错误:对象pivotcache的方法createpivottable失败。

Sub RunResourceReport()

' Create the Resource Requests Pivot Table
ActiveWorkbook.Sheets("CP Monthly Data").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "CP Monthly Data!R1C1:R486C15", Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="", TableName:="Resource Requests", DefaultVersion _
    :=xlPivotTableVersion14
ActiveWorkbook.ActiveSheet.Name = "Resource Requests"
With ActiveSheet.PivotTables("Resource Requests")
    .InGridDropZones = True
    .AllowMultipleFilters = True
    .RowAxisLayout xlTabularRow
    .TableStyle2 = "PivotStyleMedium4"
End With

' Turn off subtotals.
ActiveSheet.PivotTables("Resource Requests").PivotFields("Probability Status"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("Resource Requests").PivotFields("Project"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("Resource Requests").PivotFields("Project manager"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("Resource Requests").PivotFields("Company name"). _
    Subtotals(1) = False

ActiveSheet.PivotTables("Resource Requests").PivotFields( _
    "Workgroup Name").ClearAllFilters
ActiveSheet.PivotTables("Resource Requests").PivotFields( _
    "Workgroup Name").PivotFilters.Add Type:=xlCaptionBeginsWith, Value1:="Custom"
With ActiveSheet.PivotTables("Resource Requests").PivotFields("Workgroup Name")
    .Orientation = xlPageField
    .Position = 1
End With

With ActiveSheet.PivotTables("Resource Requests").PivotFields("Company name")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("Resource Requests").PivotFields( _
    "Probability Status").PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:="X"
With ActiveSheet.PivotTables("Resource Requests").PivotFields( _
    "Probability Status")
    .Orientation = xlRowField
    .Position = 2
End With
With ActiveSheet.PivotTables("Resource Requests").PivotFields("Project")
    .Orientation = xlRowField
    .Position = 3
End With
With ActiveSheet.PivotTables("Resource Requests").PivotFields("Project manager" _
    )
    .Orientation = xlRowField
    .Position = 4
End With
ActiveSheet.PivotTables("Resource Requests").PivotFields("Resource name"). _
    PivotFilters.Add Type:=xlCaptionBeginsWith, Value1:="*TBD"
With ActiveSheet.PivotTables("Resource Requests").PivotFields("Resource name")
    .Orientation = xlRowField
    .Position = 5
End With

ActiveSheet.PivotTables("Resource Requests").AddDataField ActiveSheet. _
    PivotTables("Resource Requests").PivotFields(Format(DateAdd("m", 0, _
    Now()), "mmmm, yyyy")), Format(DateAdd("m", 0, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Requests").AddDataField ActiveSheet. _
    PivotTables("Resource Requests").PivotFields(Format(DateAdd("m", 1, _
    Now()), "mmmm, yyyy")), Format(DateAdd("m", 1, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Requests").AddDataField ActiveSheet. _
    PivotTables("Resource Requests").PivotFields(Format(DateAdd("m", 2, _
    Now()), "mmmm, yyyy")), Format(DateAdd("m", 2, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Requests").AddDataField ActiveSheet. _
    PivotTables("Resource Requests").PivotFields(Format(DateAdd("m", 3, _
    Now()), "mmmm, yyyy")), Format(DateAdd("m", 3, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Requests").AddDataField ActiveSheet. _
    PivotTables("Resource Requests").PivotFields(Format(DateAdd("m", 4, _
    Now()), "mmmm, yyyy")), Format(DateAdd("m", 4, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Requests").AddDataField ActiveSheet. _
    PivotTables("Resource Requests").PivotFields(Format(DateAdd("m", 5, _
    Now()), "mmmm, yyyy")), Format(DateAdd("m", 5, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Requests").AddDataField ActiveSheet. _
    PivotTables("Resource Requests").PivotFields(Format(DateAdd("m", 6, _
    Now()), "mmmm, yyyy")), Format(DateAdd("m", 6, Now()), "mmm"), xlSum

ActiveSheet.PivotTables("Resource Requests").PivotFields("Probability Status"). _
    AutoSort xlDescending, "Probability Status"
ActiveSheet.PivotTables("Resource Requests").PivotFields("Resource name"). _
    AutoSort xlAscending, "Resource name"
ActiveWorkbook.ShowPivotTableFieldList = False

' Create the Resource Monthly Detail Pivot Table
ActiveWorkbook.Sheets("CP Monthly Data").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "CP Monthly Data!R1C1:R486C15", Version:=xlPivotTableVersion14). _
    CreatePivotTable TableDestination:="", TableName:="Resource Monthly Detail" _
    , DefaultVersion:=xlPivotTableVersion14
ActiveWorkbook.ActiveSheet.Name = "Resource Monthly Detail"
With ActiveSheet.PivotTables("Resource Monthly Detail")
    .InGridDropZones = True
    .AllowMultipleFilters = True
    .RowAxisLayout xlTabularRow
    .TableStyle2 = "PivotStyleMedium2"
End With

' Turn off subtotals.
ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Workgroup Name"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Project"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Project manager"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Resource name"). _
    Subtotals(1) = False

ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Resource name").ShowDetail = _
    False

With ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Probability Status")
    .Orientation = xlPageField
    .Position = 1
    .EnableMultiplePageItems = True
    .PivotItems("X - Lost - 0%").Visible = False
    .PivotItems("X - On Hold - 0%").Visible = False
End With
ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields( _
    "Workgroup Name").PivotFilters.Add Type:=xlCaptionBeginsWith, Value1:="Custom"
With ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Workgroup Name")
    .Orientation = xlRowField
    .Position = 1
End With
With ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Resource name")
    .Orientation = xlRowField
    .Position = 2
End With
With ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Project")
    .Orientation = xlRowField
    .Position = 3
End With

ActiveSheet.PivotTables("Resource Monthly Detail").AddDataField ActiveSheet.PivotTables( _
    "Resource Monthly Detail").PivotFields(Format(DateAdd("m", 0, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 0, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Monthly Detail").AddDataField ActiveSheet.PivotTables( _
    "Resource Monthly Detail").PivotFields(Format(DateAdd("m", 1, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 1, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Monthly Detail").AddDataField ActiveSheet.PivotTables( _
    "Resource Monthly Detail").PivotFields(Format(DateAdd("m", 2, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 2, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Monthly Detail").AddDataField ActiveSheet.PivotTables( _
    "Resource Monthly Detail").PivotFields(Format(DateAdd("m", 3, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 3, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Monthly Detail").AddDataField ActiveSheet.PivotTables( _
    "Resource Monthly Detail").PivotFields(Format(DateAdd("m", 4, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 4, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Monthly Detail").AddDataField ActiveSheet.PivotTables( _
    "Resource Monthly Detail").PivotFields(Format(DateAdd("m", 5, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 5, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("Resource Monthly Detail").AddDataField ActiveSheet.PivotTables( _
    "Resource Monthly Detail").PivotFields(Format(DateAdd("m", 6, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 6, Now()), "mmm"), xlSum

ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Workgroup Name").AutoSort _
    xlAscending, "Workgroup Name"
ActiveSheet.PivotTables("Resource Monthly Detail").PivotFields("Resource name").AutoSort _
    xlAscending, "Resource name"
ActiveWorkbook.ShowPivotTableFieldList = False

' Create the Resource Monthly Detail By Project Pivot Table
ActiveWorkbook.Sheets("CP Monthly Data").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "CP Monthly Data!R1C1:R486C15", Version:=xlPivotTableVersion14). _
    CreatePivotTable TableDestination:="", TableName:="RMD By Project" _
    , DefaultVersion:=xlPivotTableVersion14
ActiveWorkbook.ActiveSheet.Name = "Resource Detail By Project"
With ActiveSheet.PivotTables("RMD By Project")
    .InGridDropZones = True
    .AllowMultipleFilters = True
    .RowAxisLayout xlTabularRow
    .TableStyle2 = "PivotStyleMedium6"
End With

' Turn off subtotals.
ActiveSheet.PivotTables("RMD By Project").PivotFields("Workgroup Name"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("RMD By Project").PivotFields("Project"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("RMD By Project").PivotFields("Probability Status"). _
    Subtotals(1) = False
ActiveSheet.PivotTables("RMD By Project").PivotFields("Resource name"). _
    Subtotals(1) = False

ActiveSheet.PivotTables("RMD By Project").PivotFields("Project").ShowDetail = _
    False

ActiveSheet.PivotTables("RMD By Project").PivotFields( _
    "Workgroup Name").PivotFilters.Add Type:=xlCaptionBeginsWith, Value1:="Custom"
With ActiveSheet.PivotTables("RMD By Project").PivotFields("Workgroup Name")
    .Orientation = xlPageField
    .Position = 1
End With
ActiveSheet.PivotTables("RMD By Project").PivotFields( _
    "Probability Status").PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:="X"
With ActiveSheet.PivotTables("RMD By Project").PivotFields("Probability Status" _
    )
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("RMD By Project").PivotFields("Probability Status"). _
    AutoSort xlDescending, "Probability Status"
With ActiveSheet.PivotTables("RMD By Project").PivotFields("Project")
    .Orientation = xlRowField
    .Position = 2
End With
With ActiveSheet.PivotTables("RMD By Project").PivotFields("Resource name")
    .Orientation = xlRowField
    .Position = 3
End With
ActiveSheet.PivotTables("RMD By Project").AddDataField ActiveSheet.PivotTables( _
    "RMD By Project").PivotFields(Format(DateAdd("m", 0, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 0, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("RMD By Project").AddDataField ActiveSheet.PivotTables( _
    "RMD By Project").PivotFields(Format(DateAdd("m", 1, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 1, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("RMD By Project").AddDataField ActiveSheet.PivotTables( _
    "RMD By Project").PivotFields(Format(DateAdd("m", 2, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 2, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("RMD By Project").AddDataField ActiveSheet.PivotTables( _
    "RMD By Project").PivotFields(Format(DateAdd("m", 3, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 3, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("RMD By Project").AddDataField ActiveSheet.PivotTables( _
    "RMD By Project").PivotFields(Format(DateAdd("m", 4, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 4, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("RMD By Project").AddDataField ActiveSheet.PivotTables( _
    "RMD By Project").PivotFields(Format(DateAdd("m", 5, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 5, Now()), "mmm"), xlSum
ActiveSheet.PivotTables("RMD By Project").AddDataField ActiveSheet.PivotTables( _
    "RMD By Project").PivotFields(Format(DateAdd("m", 6, Now()), "mmmm, yyyy")), _
    Format(DateAdd("m", 6, Now()), "mmm"), xlSum
ActiveWorkbook.ShowPivotTableFieldList = False

ActiveWorkbook.Sheets("Executive Summary").Select
Range("A1:M66").Value = Range("A1:M66").Value

End Sub

“概率状态”字段中使用的PivotFilter工作正常,但我只需要显示以“自定义”开头的工作组名称的项目。

我尝试使用以下方法将Visible更改为false:

For Each PivItem In ActiveSheet.PivotTables("Resource Requests").PivotFields("Workgroup Name").PivotItems
    Select Case PivItem.Name
    Case "Custom*"
        PivItem.Visible = True
    Case Else
        PivItem.Visible = False
    End Select
Next PivItem

但是当它到达该行以将Visible更改为False时,会收到错误“无法设置pivotitem类的可见属性”。

请帮助我解决其中一个或两个问题!

1 个答案:

答案 0 :(得分:0)

答案可能在于客户端使用的excel版本。尝试更改此内容:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "CP Monthly Data!R1C1:R486C15", Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="", TableName:="Resource Requests", DefaultVersion _
    :=xlPivotTableVersion14

对此,在他们的程序副本中:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "CP Monthly Data!R1C1:R486C15", Version:=xlPivotTableVersion12).CreatePivotTable _
    TableDestination:="", TableName:="Resource Requests", DefaultVersion _
    :=xlPivotTableVersion12