2010-06-17 113 views
0

我創建了xlt excel模板,該模板在兼容模式下可以在Excel 2007中正常工作,並且在兼容性檢查時不顯示任何錯誤。該模板運行多個創建數據透視表和圖表的宏。Excel 2003中的Excel宏運行時錯誤428

當一個同事試圖在Excel 2003上運行相同的xlt時,他們得到運行時錯誤428(對象不支持此屬性或方法)。

此時運行時錯誤失敗;

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
    "raw!R1C1:R65536C37", Version:=xlPivotTableVersion10).CreatePivotTable _ 
    TableDestination:="Frontpage!R7C1", TableName:="PivotTable2", _ 
    DefaultVersion:=xlPivotTableVersion10 

任何幫助,將不勝感激。

這是完整的宏;

Sub Auto_Open() 
' 
' ImportData Macro 
' Macro to import data, Data must be in your local D: Drive and named raw.csv 
' 

' 
    Sheets("raw").Select 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;d:\raw.csv", Destination:=Range _ 
     ("$A$1")) 
     .Name = "raw_1" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 850 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = False 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
    End With 
' 
' AddMonthColumn Macro 
' 
' 
    Sheets("raw").Select 
    Range("AK1").Select 
    ActiveCell.FormulaR1C1 = "Month" 
    Range("AK2").FormulaR1C1 = "=DATE(YEAR(RC[-36]),MONTH(RC[-36]),1)" 
    LastRow = ActiveSheet.UsedRange.Rows.Count 
    Range("AK2").AutoFill Destination:=Range("AK2:AK" & LastRow) 
    Columns("AK:AK").EntireColumn.AutoFit 
    Columns("AK:AK").Select 
    Selection.NumberFormat = "mmmm" 
    With Selection 
     .HorizontalAlignment = xlCenter 
    End With 
    Columns("AK:AK").EntireColumn.AutoFit 
    Selection.Copy 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
' 
' Add Report Information [Text] 
' 
    Sheets("Frontpage").Select 
    Range("A2:N2").Select 
    Selection.Merge 
    ActiveCell.FormulaR1C1 = "Service Activity Report" 
    With Selection.Font 
     .Size = 20 
    End With 
    Range("A3:N3").Select 
    Selection.Merge 
    ActiveCell.FormulaR1C1 = InputBox("Customer Name") 
     With Selection 
      .HorizontalAlignment = xlCenter 
      .VerticalAlignment = xlCenter 
     End With 
    Range("A4:N4").Select 
    Selection.Merge 
    ActiveCell.FormulaR1C1 = InputBox("Date Range dd/mm/yyyy - dd/mm/yyyy") 
     With Selection 
      .HorizontalAlignment = xlCenter 
      .VerticalAlignment = xlCenter 
     End With 
' 
' IncidentsbyPriority Macro 
' 

' 
    Sheets("Frontpage").Select 
    Range("A7").Select 
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "raw!R1C1:R65536C37", Version:=xlPivotTableVersion10).CreatePivotTable _ 
     TableDestination:="Frontpage!R7C1", TableName:="PivotTable2", _ 
     DefaultVersion:=xlPivotTableVersion10 
    Sheets("Frontpage").Select 
    Cells(7, 1).Select 
    ActiveSheet.Shapes.AddChart.Select 
    ActiveChart.SetSourceData Source:=Range("Frontpage!$A$7:$H$22") 
    ActiveChart.ChartType = xlColumnClustered 
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Priority") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables(_ 
     "PivotTable2").PivotFields("Case ID"), "Count of Case ID", xlCount 
    ActiveChart.Parent.Name = "IncidentsbyPriority" 
    ActiveChart.ChartTitle.Text = "Incidents by Priority" 
    Dim RngToCover As Range 
    Dim ChtOb As ChartObject 
    Set RngToCover = ActiveSheet.Range("D7:L16") 
    Set ChtOb = ActiveSheet.ChartObjects("IncidentsbyPriority") 
    ChtOb.Height = RngToCover.Height ' resize 
    ChtOb.Width = RngToCover.Width ' resize 
    ChtOb.Top = RngToCover.Top ' reposition 
    ChtOb.Left = RngToCover.Left ' reposition 
' 
' IncidentbyMonth Macro 
' 

' 
    Sheets("Frontpage").Select 
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "raw!R1C1:R65536C37", Version:=xlPivotTableVersion10).CreatePivotTable _ 
     TableDestination:="Frontpage!R18C1", TableName:="PivotTable4", _ 
     DefaultVersion:=xlPivotTableVersion10 
    Sheets("Frontpage").Select 
    Cells(18, 1).Select 
    ActiveSheet.Shapes.AddChart.Select 
    ActiveChart.SetSourceData Source:=Range("Frontpage!$A$18:$H$38") 
    ActiveChart.ChartType = xlColumnClustered 
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Month") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
    ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables(_ 
     "PivotTable4").PivotFields("Case ID"), "Count of Case ID", xlCount 
    ActiveChart.Parent.Name = "IncidentbyMonth" 
    ActiveChart.ChartTitle.Text = "Incidents by Month" 
    Dim RngToCover2 As Range 
    Dim ChtOb2 As ChartObject 
    Set RngToCover2 = ActiveSheet.Range("D18:L30") 
    Set ChtOb2 = ActiveSheet.ChartObjects("IncidentbyMonth") 
    ChtOb2.Height = RngToCover2.Height ' resize 
    ChtOb2.Width = RngToCover2.Width ' resize 
    ChtOb2.Top = RngToCover2.Top ' reposition 
    ChtOb2.Left = RngToCover2.Left ' reposition 

' 
' IncidentbyCategory Macro 
' 

' 
    Sheets("Frontpage").Select 
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "raw!R1C1:R65536C37", Version:=xlPivotTableVersion10).CreatePivotTable _ 
     TableDestination:="Frontpage!R38C1", TableName:="PivotTable6", _ 
     DefaultVersion:=xlPivotTableVersion10 
    Sheets("Frontpage").Select 
    Cells(38, 1).Select 
    ActiveSheet.Shapes.AddChart.Select 
    ActiveChart.SetSourceData Source:=Range("Frontpage!$A$38:$H$119") 
    ActiveChart.ChartType = xlColumnClustered 
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("Category 2") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
    With ActiveSheet.PivotTables("PivotTable6").PivotFields("Category 3") 
     .Orientation = xlPageField 
     .Position = 1 
    End With 
    ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables(_ 
     "PivotTable6").PivotFields("Case ID"), "Count of Case ID", xlCount 
    ActiveChart.Parent.Name = "IncidentbyCategory" 
    ActiveChart.ChartTitle.Text = "Incidents by Category" 
    Dim RngToCover3 As Range 
    Dim ChtOb3 As ChartObject 
    Set RngToCover3 = ActiveSheet.Range("D38:L56") 
    Set ChtOb3 = ActiveSheet.ChartObjects("IncidentbyCategory") 
    ChtOb3.Height = RngToCover3.Height ' resize 
    ChtOb3.Width = RngToCover3.Width ' resize 
    ChtOb3.Top = RngToCover3.Top ' reposition 
    ChtOb3.Left = RngToCover3.Left ' reposition 
' 
' IncidentsbySiteandPriority Macro 
' 

' 
    Sheets("Frontpage").Select 
    Range("A71").Select 
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "raw!R1C1:R65536C37", Version:=xlPivotTableVersion10).CreatePivotTable _ 
     TableDestination:="Frontpage!R71C1", TableName:="PivotTable3", _ 
     DefaultVersion:=xlPivotTableVersion10 
    Sheets("Frontpage").Select 
    Cells(71, 1).Select 
    ActiveSheet.Shapes.AddChart.Select 
    ActiveChart.SetSourceData Source:=Range("Frontpage!$A$71:$H$90") 
    ActiveChart.ChartType = xlColumnClustered 
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Site Name") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Priority") 
     .Orientation = xlColumnField 
     .Position = 1 
    End With 
    ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables(_ 
     "PivotTable3").PivotFields("Case ID"), "Count of Case ID", xlCount 
    ActiveChart.Parent.Name = "IncidentbySiteandPriority" 
' ActiveChart.ChartTitle.Text = "Incidents by Site and Priority" 
    Dim RngToCover4 As Range 
    Dim ChtOb4 As ChartObject 
    Set RngToCover4 = ActiveSheet.Range("H71:O91") 
    Set ChtOb4 = ActiveSheet.ChartObjects("IncidentbySiteandPriority") 
    ChtOb4.Height = RngToCover4.Height ' resize 
    ChtOb4.Width = RngToCover4.Width ' resize 
    ChtOb4.Top = RngToCover4.Top ' reposition 
    ChtOb4.Left = RngToCover4.Left ' reposition 
    Columns("A:G").Select 
    Range("A52").Activate 
    Columns("A:G").EntireColumn.AutoFit 

End Sub 

回答

0

嘗試ActiveWorkbook.PivotCaches.Add()代替ActiveWorkbook.PivotCaches.Create()

+1

這工作,我不得不從源數據刪除的版本太多,但它的工作原理 (SourceType中:= xlDatabase,SourceData:= 「原始R1C1:R65536C37」 ,Version:= xlPivotTableVersion10) Thanks a mil – Adam 2010-06-18 10:51:45