PivotCaches.add错误5 – 2003到2010macros

我有一个Excel 2003 .xls文件,我试图在Excel 2010中运行。我首先将该文件保存为.xlsm,并将信任目录添加到信任中心中。 我得到一个错误代码(如箭头所示)。 注意:如果我将PivotTableVersion更改为12,它仍然给我同样的错误。 代码如下。

Sub Create_pivot() Wbname = ActiveWorkbook.Name ' Insert columns to make room for pivot table Columns("A:I").Select Selection.Insert Shift:=xlToRight myData = Sheets(ActiveSheet.Name).[J1].CurrentRegion.Address mySheet = ActiveSheet.Name & "!" tableDest = "[" & Wbname & "]" & mySheet & "R1C1" >>>> ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _ mySheet & myData).CreatePivotTable TableDestination:=tableDest, TableName _ :="RTP_alerts", DefaultVersion:=xlPivotTableVersion10 With ActiveSheet.PivotTables("RTP_alerts").PivotFields("Application") .Orientation = xlRowField .Position = 1 End With With ActiveSheet.PivotTables("RTP_alerts").PivotFields("Object") .Orientation = xlRowField .Position = 2 End With ActiveSheet.PivotTables("RTP_alerts").AddDataField ActiveSheet.PivotTables( _ "RTP_alerts").PivotFields("Alerts"), "Count of Alerts", xlCount ActiveWorkbook.ShowPivotTableFieldList = False Application.CommandBars("PivotTable").Visible = False Columns("G:I").Select Selection.Delete Shift:=xlToLeft Range("D2").Select ActiveCell.FormulaR1C1 = "Owner" Range("E2").Select ActiveCell.FormulaR1C1 = "Problem Ticket" Columns("E:E").ColumnWidth = 13 Range("F2").Select ActiveCell.FormulaR1C1 = "Comments" Columns("F:F").ColumnWidth = 48 End Sub 

他们改变了PivotCaches的对象模型。 您需要在2007 – 2010年(使用VBA版本7,而不是版本6)的方法是

 PivotCaches.Create 

您可以使用条件编译来创build可以在两者中工作的代码,如下所示:

 Dim pc As PivotCache Dim pt As PivotTable Dim lVBAVer As Long lVBAVer = CLng(Application.VBE.Version) #If lVBAVer <= 6 Then Set pc = ActiveWorkbook.PivotCaches.Add(xlDatabase, Sheet1.UsedRange) #Else Set pc = ActiveWorkbook.PivotCaches.create(xldtatabase, Sheet1.UsedRange) #End If Set pt = pc.CreatePivotTable(Sheet2.Range("A3")) 

If/EndIf关键字之前的散列表示在使用该版本中不存在的方法时,您将不会收到编译错误,但仍会执行。

我最近从Excel 2003升级到Excel 2010.我有一个包含macros的许多.xls文件类似的问题:一个通用的错误[-2147417848(80010108)]的指示,如下所示:

 ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _ NomeFoglioDett & "!R1C1:R" & UltimaRiga & "C18").CreatePivotTable TableDestination:="", _ TableName:="Tabella_pivot1", DefaultVersion:=xlPivotTableVersion10 

我尝试了各种操作:保存为.xlsm或.xls(97-2003),从PivotCaches.Add修改为PivotCaches.Create,将DefaultVersion从xlPivotTablesVersion10更改为xlPivotTablesVersion12或14。 最后,我意识到这可能是在创build数据透视表之前。 这里的指示不起作用:

 Dim Anno As String Dim Percorso As String Dim NomeFileOut As String Dim NomeFoglioDett As String Dim UltimaRiga As String Anno = "2013" Percorso = ActiveWorkbook.Path & "\" NomeFileOut = "MyName1." & Anno & ".0m.1.BIn.xls" NomeFoglioDett = " MyName2." & Anno & ".0m.1.Tp2" '. '. '. '================= Creazione Tabella Pivot 'Riapro il Batch Input in formato Excel - foglio dei tipi 2 Workbooks.Open Filename:=Percorso & NomeFileOut 'Cerco l'ultima riga piena UltimaRiga = Range("A60000").End(xlUp).Row 'Creo la tabella Pivot 'totali Importi segnati per Descrizioni ridotte ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _ NomeFoglioDett & "!R1C1:R" & UltimaRiga & "C18").CreatePivotTable TableDestination:="", _ TableName:="Tabella_pivot1", DefaultVersion:=xlPivotTableVersion10 ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) ActiveSheet.Cells(3, 1).Select With ActiveSheet.PivotTables("Tabella_pivot1").PivotFields("Mese") .Orientation = xlRowField .Position = 1 End With With . . . 

这里有相同的修改说明 – Sheets(2).select – 解决了保存在.xls和.xlsm中的文件的问题:

 '================= Creazione Tabella Pivot 'Riapro il Batch Input in formato Excel - foglio dei tipi 2 Workbooks.Open Filename:=Percorso & NomeFileOut Sheets(2).Select 'Cerco l'ultima riga piena UltimaRiga = Range("A60000").End(xlUp).Row 'Creo la tabella Pivot 'totali Importi segnati per Descrizioni ridotte ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _ NomeFoglioDett & "!R1C1:R" & UltimaRiga & "C18").CreatePivotTable TableDestination:="", _ TableName:="Tabella_pivot1", DefaultVersion:=xlPivotTableVersion10 ActiveSheet.PivotTableWizard . . . 

看来,Excel 2010接受以旧方式创build数据透视表,但希望打开一个.xls文件更精确:它想知道哪个表必须被读取。