我正在使用 Excel 2010 并在 VBA 中编写宏。我有 2 个问题:1-我的大多数 PivotFilters 实际上并没有过滤,并且 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 类的可见属性”。
请帮助解决我的一个或两个问题!