我有一个 Excel 2003 .xls文件,我正在尝试在 Excel 2010 中运行该文件。我首先将文件另存为 .xlsm,并在信任中心将目录添加为受信任目录。我收到一个错误代码(由下面的箭头指示)。备注:如果我将数据透视表版本更改为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
可以使用条件编译来创建适用于两者的代码,如下所示:
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 2010升级到Excel 2003。我对许多包含宏.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。什么都没用。最后,我意识到它可能只是在创建数据透视表之上的东西。以下是不起作用的说明:
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 和 .xls 中的文件的问题:
'================= 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接受以旧方式创建数据透视表,但希望在打开.xls文件时具有更高的精度:它想知道必须读取哪个工作表。