2017-03-27 236 views
2

我想根據同一工作簿中的數據集(包含在工作表中)創建數據透視表。運行時錯誤1004「無法打開數據透視表源文件」

當我運行宏時,工作簿打開。數據集來自在Access中運行查詢,然後將其導出到Excel。我也嘗試在運行宏之前保存工作簿。我使用Excel 2016

這是我的代碼:

Sub BusinessInteligenceCreatePivotTable() 
    Dim PivotSheet As Worksheet 
    Dim pvtCache As PivotCache 
    Dim pvt As PivotTable 

    'Determine the data range you want to pivot 
    Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=wsPartsMachines.Name & "'!" & wsPartsMachines.Range("A1").CurrentRegion.Address, Version:=xlPivotTableVersion15) 

    'Create a new worksheet 
    With ThisWorkbook 
     Set PivotSheet = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 
     PivotSheet.Name = "Production Schedule" 
    End With 

    PivotSheet.Activate 
    Range("A1").Select 

    'Create Pivot table from Pivot Cache 
    'Set pvt = pvtCache.CreatePivotTable(TableDestination:=ActiveCell, TableName:="ProductionSchedule") 
    Set pvt = PivotSheet.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=ActiveCell, TableName:="ProdSched") 
End Sub 

最後兩行產生相同的錯誤消息。 「運行時錯誤1004.不能打開數據透視表源文件'C:\ Users ...'」。

有誰知道如何解決這個問題? 謝謝。

編輯 當我錄製一個宏時,VBA給了我這個代碼(它有效)。

Sub BusinessInteligenceCreatePivotTable() 
    Dim PivotSheet As Worksheet 
    With ThisWorkbook 
     Set PivotSheet = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 
     PivotSheet.Name = "Production Schedule" 
    End With 

    PivotSheet.Activate 
    Range("A1").Select 

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
     "Parts & Machines2!R1C1:R1328C14", Version:=6).CreatePivotTable _ 
     TableDestination:=ActiveCell, TableName:="PivotTable1", DefaultVersion:=6 

End Sub 

我想我的SourceData的範圍是動態設置的。我的努力生成(使用debug.print):'Parts & Machines2'!R1C1:R1328C14 它似乎不同於宏記錄:「Parts & Machines2!R1C1:R1328C14」。

這種差異是否會產生我無法找到源數據的錯誤?

數據的屏幕截圖。 enter image description here工作表數據

回答

0

我不太確定您在哪裏定義wsPartsMachines as Worksheet以及您在哪裏設置它。

但是,錯誤是在該行:

Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=wsPartsMachines.Name & "'!" & wsPartsMachines.Range("A1").CurrentRegion.Address, Version:=xlPivotTableVersion15) 

如果之後添加一行:

Debug.Print pvtCache.SourceData 

您將在即時窗口得到'Sheet3'''!R1C1:R6C3 - 你有一個'太多。 (我曾用 「表Sheet 3」 作爲我SourceData) 嘗試修改該行:

Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=wsPartsMachines.Name & "!" & wsPartsMachines.Range("A1").CurrentRegion.Address) 

編輯1:嘗試不同的方法,把數據源照片直接作爲Range

Dim pvtDataRng As Range 

Set wsPartsMachines = Sheets("Parts & Machines2") 
' set the Pivot Table Data Source Range 
Set pvtDataRng = wsPartsMachines.Range("A1").CurrentRegion 

Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pvtDataRng) 

'Create Pivot table from Pivot Cache 
Set pvt = PivotSheet.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=ActiveCell, TableName:="ProdSched") 
+0

'零件和Machines2' '' R1C1:!R1328C14 '零件和Machines2' R1C1:R1328C14 – Energizer1

+0

wsPartsMachines是工作表的名稱,即而不是 「工作表Sheet」 我在VBA編輯器更名爲wsPartsMachines。 Debug.Print pvtCache.SourceData在不進行任何原始代碼編輯的情況下生成此代碼: 'Parts&Machines2'''!R1C1:零件和Machines2:R1328C14 Debug.Print pvtCache.SourceData「 從原來的代碼中刪除時‘‘‘生成這個’R1C1:R1328C14 運行此代碼(’’」去掉),我收到此錯誤信息「Set pvt =」line insted: 「運行時錯誤1004。數據透視表字段名稱無效。要創建數據透視表,您必須使用組織爲列表的數據...」 – Energizer1

+0

@ Energizer1這意味着您的數據不是按照構建數據透視表的方式進行組織。你可以在上面的文章中添加你的數據的屏幕截圖嗎? –

0

此代碼有效。仍然不確定爲什麼SourceData不起作用。 Shai Rado建議它的代碼稍作修改。以下是代碼。

Sub BusinessInteligenceCreatePivotTable() 

    Dim PivotSheet As Worksheet 
    Dim pvtCache As PivotCache 
    Dim pvtTable As PivotTable 
    Dim ws1PartMachines As Worksheet 
    Dim pvtDataRng As Range 

    'Determine the data range you want to pivot 
    Set ws1PartsMachines = Worksheets("Parts & Machines2") 
    ' set the Pivot Table Data Source Range 
    Set pvtDataRng = ws1PartsMachines.Range("A1").CurrentRegion 

    Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pvtDataRng) 
    Debug.Print pvtCache.SourceData 

    'Create a new worksheet 
    With ThisWorkbook 
     Set PivotSheet = .Sheets.Add(After:=.Sheets(.Sheets.Count)) 
     PivotSheet.Name = "Production Schedule2" 
    End With 

    PivotSheet.Activate 
    Range("A1").Select 

    'Create Pivot table from Pivot Cache 
    Set pvtTable = PivotSheet.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=ActiveCell, TableName:="ProdSched") 

End Sub 
+0

我應該在哪裏放這個代碼? –

相關問題