我正在使用excel 2013,并且在调用PivotCaches.Add时收到无效的过程调用或参数
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set ActiveWorkbook = objExcel.Workbooks.Open("C:\Users\srujan\Desktop\TIME REPORT\fresh\25_Report Time Booking_25.xls")
ActiveWorkbook.Sheets("25_Report Time Booking_25").Select
' Set the range to be pivoted to be called PivotRange
Set PivotTopLeft = ActiveWorkbook.Parent.Worksheets("25_Report Time Booking_25").Range("A1")
Set PivotTopRight = PivotTopLeft.Range("G1")
Set PivotTop = ActiveWorkbook.Parent.Worksheets("25_Report Time Booking_25").Range(PivotTopLeft, PivotTopRight)
Set MyPivotRange = ActiveWorkbook.Parent.Worksheets("25_Report Time Booking_25").Range(PivotTop, PivotTop.Range("G78"))
' Create the pivot table
'ActiveWorkbook.CutCopyMode = False
ActiveWorkbook.Parent.Worksheets.Add
MyPivotRangeName = "'" & MyPivotRange.Parent.Name & "'" & "!" & MyPivotRange.Address(ReferenceStyle = xlR1C1)
MsgBox (MyPivotRangeName)
Set MyPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType = xlDatabase, SourceData = MyPivotRangeName)
MyPivotCache.CreatePivotTable TableDestination = (ActiveWorkbook.Parent.Worksheets("Sheet1").Range("A3")), TableName = "PivotTable1"
ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").PivotTableWizard
ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").SmallGrid = False
ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").AddFields RowFields = "Activity Type", PageFields = "User"
With ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Effort Spent (Hrs)" & Chr(10) & "($)")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
End With
With ActiveWorkbook.Parent.Worksheets("Sheet1").PivotTables("PivotTable1").PivotFields("Activity Type")
.PivotItems("ANALYSIS").Visible = False
.PivotItems("BUILD").Visible = False
.PivotItems("CARRIERSAP").Visible = False
.PivotItems("HOWTO").Visible = False
.PivotItems("MAINTENANC").Visible = False
.PivotItems("MAINTENANCE").Visible = False
.PivotItems("REVIEW").Visible = False
.PivotItems("STX-SCRIPT").Visible = False
.PivotItems("(blank)").Visible = False
End With
答案 0 :(得分:0)
您收到该错误的原因是您应该使用.Create
代替.Add
Set MyPivotCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, MyPivotRangeName)
修改强>
根据您提出的问题,您的代码可以像这样优化(UNTESTED)
Sub Sample()
Dim wb As Workbook
Dim ws As Worksheet, newWs As Worksheet
Dim sFile As String, MyPivotRangeName As String
Dim MyPivotRange As Range
Dim pt As PivotTable
Dim MyPivotCache As PivotCache
sFile = "C:\Users\srujan\Desktop\TIME REPORT\fresh\25_Report Time Booking_25.xls"
Set wb = Workbooks.Open(sFile)
Set ws = wb.Sheets("25_Report Time Booking_25")
Set newWs = wb.Worksheets.Add
With ws
Set MyPivotRange = .Range("A1:G78")
MyPivotRangeName = "'" & ws.Name & "'!" & MyPivotRange.Address(ReferenceStyle = xlR1C1)
Set MyPivotCache = wb.PivotCaches.Create( _
SourceType = xlDatabase, _
SourceData = MyPivotRangeName)
Set pt = MyPivotCache.CreatePivotTable( _
tabledestination:=(newWs.Name & "!R3C1"), _
tablename:="PivotTable1")
'
'~~> Rest of the code
'
End With
End Sub