0
我正在使用Excel 2010並在VBA中編寫宏。我有兩個問題:1-我的PivotFilters大部分實際上並沒有過濾,而且2我爲這個宏創建了一個可以在我的機器上正常工作的Excel宏模板,但是如果通過電子郵件發送模板並嘗試運行宏在另一臺機器上我得到錯誤:方法createpivottable對象pivotcache失敗。爲什麼我的PivotFilters不能正常工作,爲什麼我的宏只能在我的電腦上工作?
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的概率狀態字段用於工作得很好,但我只需要表明,「自定義」的工作組名稱開頭的項目。
我試圖改變可見的利用虛假:
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
但隨後得到錯誤「無法設置pivotitem類的有形財產」,當它擊中改變可見的假線。
請幫助解決我的一個或兩個問題!