首页 文章

运行时错误1004“无法打开数据透视表源文件”

提问于
浏览
2

我想基于同一工作簿中的数据集(包含在工作表中)创建数据透视表 .

运行宏时,工作簿是打开的 . 数据集来自在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” .

这种差异会产生我无法找到源数据的错误吗?

数据截图 .

Worksheet Data

2 回答

  • 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 - 您有一个 ' 太多了 . (我使用"Sheet3"作为我的 SourceData )尝试将此行修改为:

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

    Edit 1 :尝试不同的方法,将数据源direclty作为 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

    这段代码有效 . 仍然不确定为什么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
    

相关问题