首页 文章

在从SQL Server DB更新的单独工作表中的数据之后自动刷新Excel 2007数据透视表

提问于
浏览
2

Background:

我在Excel中有三张纸 .

  • 包含数据透视表的摘要表,该数据表从下面的#2中提取数据

  • 从SQL Server数据库中提取数据的数据表 . (拉出类别类型和美元值)

  • 一张工作表,其中包含用于刷新数据的按钮,最理想的是刷新数据透视表 .

Problem: 当我单击按钮时,数据表正确刷新但数据表没有 . 此工作簿自动在凌晨5点运行,并以PDF格式将结果发送给人员,因此我必须找到一种方法,在生成,发送PDF和关闭工作簿之前刷新数据透视表 .

What I have tried: 首先,单击该按钮时,它会运行:

ActiveWorkbook.RefreshAll
Application.CalculateFull

它确实更新了数据表,而不是数据表 .

我试过了:

  • 在上述两个命令之间和之后添加以下命令无效:
Sheets("Summary").PivotTables("PivotTable6").PivotCache.Refresh
Sheets("Summary").PivotTables("PivotTable6").RefreshTable
  • 我尝试过两次(背对背)运行这两个命令,如下所示:
ActiveWorkbook.RefreshAll
Application.CalculateFull
ActiveWorkbook.RefreshAll
Application.CalculateFull

希望它能在第一次运行中获得数据,并在第二次运行中成功进行数据透视刷新 . 没工作 .

  • 我试图欺骗Excel以为我关闭并重新打开工作簿然后重新刷新:
ThisWorkbook.Saved = True
 Workbooks(1).Activate
 ActiveWorkbook.RefreshAll
 Application.CalculateFull

我尝试这个的原因是因为在运行工作簿(数据库没有刷新)后,我保存并关闭 . 我重新打开(枢轴仍然错误,数据是正确的),我重新运行,(数据再次正确,相同),现在枢轴是正确的 . 所以我希望能够模拟这个 .

在这一点上,我想不出别的什么 . 我不认为Excel可以做到这一点 . 还有一件事 . 最初我们将数据直接从SQL Server DB直接进入数据透视表,但我们不断得到这些错误,所以我们将采取不同的方法:

  • 已删除部分: /xl/pivotTables/pivotTable1.xml 部分 . (数据透视表视图)

  • 已删除部分: /xl/pivotTables/pivotTable5.xml 部分 . (数据透视表视图)

  • 已删除记录: /xl/workbook.xml 部分(工作簿)中的工作簿属性

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
     <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
        <logFileName>error014800_01.xml</logFileName> 
        <summary>Errors were detected in file 'T:\Reports\RP\Archive\Historical Excel\01-11\RP_01-07-11.xlsm'</summary> 

        <removedParts summary="Following is a list of removed parts:">
            <removedPart>Removed Part: /xl/pivotTables/pivotTable1.xml part. (PivotTable view)</removedPart> 
            <removedPart>Removed Part: /xl/pivotTables/pivotTable5.xml part. (PivotTable view)</removedPart> 
        </removedParts>

        <removedRecords summary="Following is a list of removed records:">
           <removedRecord>Removed Records: Workbook properties from /xl/workbook.xml part (Workbook)</removedRecord> 
        </removedRecords>
     </recoveryLog>

任何帮助是极大的赞赏 .

3 回答

  • 1

    您可以使用工作表更改的事件来触发数据透视表上的刷新 .

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        On Error GoTo ErrHandler
        Application.EnableEvents = False
    
        'Check to see if the worksheet range raising this event overlaps the range occupied by our data dump
        If (Not (Intersect(Target, ActiveSheet.ListObjects("DATA_TABLE_NAME_HERE").Range) Is Nothing)) Then
    
            'If it does, then refesh the pivot tables that depend on this data (not automatic, name each table pivot table explicity)
            ActiveSheet.PivotTables("PIVOT_TABLE_NAME_HERE").RefreshTable
    
        End If
    
    ErrHandler:
        Application.EnableEvents = True
    End Sub
    

    您可能需要将 ActiveSheet 替换为 Sheets("whatever") ,具体取决于工作簿的构建方式 .

  • 1

    我一直处于同样的情况,我发现这是由于在连接上启用了Background查询 .

    我使用此配置在大多数工作表中包含以下内容,以强制设置禁止后台查询 .

    Sub SetNoBackgroundQuery()
          Dim i As Integer
          Dim j As Integer
          i = ThisWorkbook.Connections.Count
          If i = 0 Then End
          For j = 1 To i
            ThisWorkbook.Connections(j).ODBCConnection.BackgroundQuery = False
        '    Debug.Print ThisWorkbook.Connections(j).Name
    
      Next j
    End Sub
    
  • 0

    我不认为这是一个好主意 - 你真的应该使用Analysis Services来刷新数据 .

    数据库/查询有多大?

    我已经使用Excel数据透视表对5tb数据库,它总是给出亚秒结果 . 因为我使用过Analysis Services .

相关问题