从SQL Server数据库更新单独的表中的数据后自动刷新Excel 2007透视表

背景:

我有三张Excel表格。

  1. 带有数据透视表的汇总表,可以从#2下拉数据
  2. 从SQL Server数据库中提取数据的数据表。 (拉类别types和美元值)
  3. 有一个button来刷新数据和理想的数据透视表的表。

问题:当我点击button时,数据表刷新正确,但数据透视表没有。 这个工作簿自动运行在凌晨5点,并将结果作为PDF发送给用户,所以我必须找出一种方法,在PDF生成,发送和工作簿closures之前进行数据透视表刷新。

我曾经试过:首先,当button被点击时,它运行:

ActiveWorkbook.RefreshAll Application.CalculateFull 

它确实正确地更新了数据表,而不是主键。

我努力了:

  • 在上面和下面两个命令之间添加以下命令无济于事:
 Sheets("Summary").PivotTables("PivotTable6").PivotCache.Refresh Sheets("Summary").PivotTables("PivotTable6").RefreshTable 
  • 我已经尝试了两次运行这两个命令(背对背),像这样:
 ActiveWorkbook.RefreshAll Application.CalculateFull ActiveWorkbook.RefreshAll Application.CalculateFull 

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

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

我之所以这样做的原因是因为在运行工作簿(透视不刷新)后,我保存并closures。 我重新打开(数据透视仍然是错误的,数据是正确的),我重新运行,(数据是正确的,是一样的),现在枢轴是正确的。 所以我希望能够模拟这个。

在这一点上,我想不出别的什么。 我认为Excel不能做到这一点。 还有一件事。 最初,我们将数据直接从SQL Server数据库传送到数据透视表中,但是我们不断得到这些错误,因此我们将采取不同的方法:

  • 删除了部分: /xl/pivotTables/pivotTable1.xml部分。 (数据透视表视图)
  • 删除了部分: /xl/pivotTables/pivotTable5.xml部分。 (数据透视表视图)
  • 删除logging:从/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> 

任何帮助是极大的赞赏。

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

 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 

您可能需要用Sheets("whatever")replaceActiveSheet Sheets("whatever")具体取决于工作簿的框架。

我一直处于相同的情况,我发现这是由于在连接上启用了后台查询。

为了强制设置不允许后台查询,我在这个configuration的大部分表单中包含以下内容。

 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 

我不认为这是一个好主意,你真的应该使用分析服务来刷新数据。

数据库/查询有多大?

我已经使用5TB数据库的Excel数据透视表,它总是给出亚秒的结果。 因为我使用了Analysis Services。