3

我有一个 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
4

2 回答 2

1

他们将对象模型更改为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意味着在使用该版本中不存在的方法时不会出现编译错误,但它仍会执行。

于 2012-08-08T15:42:54.637 回答
0

我最近从 Excel 2003 升级到 Excel 2010。我在许多包含宏的 .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 和 .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 似乎以旧方式接受数据透视表的创建,但希望在打开 .xls 文件时更精确:它想知道必须读取哪个工作表。

于 2013-06-20T12:02:55.240 回答