2017-07-16 330 views
1

我已經從數據表「Sheet1」創建了一個用於在工作表「Pivot」中製作數據透視表的宏。運行時錯誤5:Excel VBA

儘管我可以在我的系統中運行宏但在其他系統中運行,但它在運行時錯誤5處出現ActiveWorkbook.PivotCaches.Create一行。

Sub Make_Pivot() 
' 
' Make_Pivot Macro 

Sheets("Sheet1").Select 
Columns("D:G").Select 
Range("G1").Activate 

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "Sheet1!R1C4:R1048576C7", Version:=6).CreatePivotTable TableDestination:= _ 
     "Pivot!R1C1", TableName:="PivotTable1", DefaultVersion:=6 

Sheets("Pivot").Select 
Cells(1, 1).Select 
With ActiveSheet.PivotTables("PivotTable1").PivotFields("NDL") 
    .Orientation = xlRowField 
    .Position = 1 
End With 

ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables(_ 
     "PivotTable1").PivotFields("Tracking IDs"), "Count of Tracking IDs", xlCount 

Columns("A:B").Select 
Range("B1").Activate 
Selection.Copy 
Range("G13").Select 
Sheets("Count").Select 
Range("A1").Select 

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
Range("F18").Select 

End Sub 

回答

-1

嘗試以下(代碼爲註釋中說明)的動態代碼:

Option Explicit 

Sub Make_Pivot() 

' Make_Pivot Macro 

Dim WB As Workbook 
Dim ws As Worksheet 
Dim PvtSht As Worksheet 
Dim PvtTbl As PivotTable 
Dim PvtCache As PivotCache 
Dim SrcData As Range 
Dim LastRow As Long 

' set the workbook object 
Set WB = ThisWorkbook 

' set the worksheet object where the Pivot-Table will be 
Set PvtSht = ThisWorkbook.Worksheets("Pivot") 

' set the worksheet object where the Data lies 
Set ws = ThisWorkbook.Worksheets("Sheet1") 
With ws 
    LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row ' <-- last row in column "G" 
    ' set the Pivot-Cache SourceData object 
    Set SrcData = .Range("D1:G" & LastRow) 
End With 

' Create the Pivot Cache 
Set PvtCache = WB.PivotCaches.Create(SourceType:=xlDatabase, _ 
           SourceData:=SrcData.Address(False, False, xlA1, xlExternal)) 
' === FOR DEBUG ONLY === 
MsgBox PvtCache.SourceData 

' Set the Pivot Table Object (already created in previous macro run) 
On Error Resume Next 
Set PvtTbl = PvtSht.PivotTables("PivotTable1") 

On Error GoTo 0 
If PvtTbl Is Nothing Then ' <-- pivot table still doesn't exist >> need to create it 
    ' create a new Pivot Table in "Pivot" sheet, start from Cell A1 
    Set PvtTbl = PvtSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PvtSht.Range("A1"), TableName:="PivotTable1") 

    With PvtTbl 
     With .PivotFields("NDL") 
      .Orientation = xlRowField 
      .Position = 1 
     End With 

     .AddDataField .PivotFields("Tracking IDs"), "Count of Tracking IDs", xlCount 
    End With 
Else 
    ' just refresh the Pivot table, with updated Pivot Cache 
    PvtTbl.ChangePivotCache PvtCache 
    PvtTbl.RefreshTable 
End If 

' rest of your code goes here ... 

End Sub 

:有沒有必要使用這麼多SelectSelection,而是使用完全組隊參加RangeWorksheetObject s。

+0

這是給我運行時錯誤91行「SrcData = .Range(」D1:J「和LastRow)」 – Agrinthus

+0

@ Agrinthus當你運行它時,'LastRow'的值是什麼? –

+0

我的列A,B,C是空的,並且數據在列DG中存在,直到第17行。(我在編輯代碼時將J移至G) – Agrinthus