在Excel 2007 VBA中刷新数据透视表不起作用

我试图更新源数据表刷新后,从VBA数据透视表。 无论我做什么,数据透视表只会在第二次执行代码刷新。 我已经尝试了pivot.RefreshTable和pivot.Update的所有组合和顺序,添加Do事件,将Application.ScreenUpdating设置为false之前和True之后。 我正在用尽想法。 每次在SQL中修改数据时,我都必须执行一次才能在源表中查看数据,并再次在数据透视表中查看数据。

Dim c As WorkbookConnection Set c = ThisWorkbook.Connections.Item("Sheet1") sSQL = "Select * from Table1 " c.OLEDBConnection.CommandText = sSQL c.OLEDBConnection.CommandType = xlCmdSql c.Refresh For Each pivot In ThisWorkbook.Worksheets("Sheet1").PivotTables pivot.RefreshTable pivot.Update pivot.PivotCache.Refresh DoEvents pivot.RefreshTable pivot.Update pivot.PivotCache.Refresh Next 

确保连接不会在后台刷新:

 Dim c As WorkbookConnection Set c = ThisWorkbook.Connections.Item("Sheet1") sSQL = "Select * from Table1 " c.OLEDBConnection.CommandText = sSQL c.OLEDBConnection.CommandType = xlCmdSql c.OLEDBConnection.Backgroundquery = False c.Refresh For Each pivot In ThisWorkbook.Worksheets("Sheet1").PivotTables pivot.RefreshTable Next