.PivotCache.Refresh无效

时间:2016-08-25 13:34:46

标签: excel vba excel-vba

我包含了所有代码,以防有什么事情造成这种情况,但我不认为有。如果我单步执行代码,则最后一行正常工作。如果我实时运行它不会。有什么想法吗?

Sub Updatequery()
For a = 1 To 2
oldconnstring = ActiveWorkbook.Connections(a).ODBCConnection.Connection
Debug.Print oldconnstring
P1 = Left(oldconnstring, 9)
P2 = Mid(oldconnstring, InStr(10, oldconnstring, ";"), 8)
P3 = Mid(oldconnstring, (InStr(10, oldconnstring, "DATABASE") - 11), 20)
P4 = Mid(oldconnstring, (InStr(10, oldconnstring, "AUTHENTIC") - 1), 200)
O1 = Worksheets("Data").Cells(2, 4).Value
O2 = Worksheets("Data").Cells(2, 5).Value
newconnstring = P1 & O2 & P2 & O1 & P3 & Replace(O2, "_", "") & P4
Debug.Print oldconnstring
With ActiveWorkbook.Connections(a).ODBCConnection
    .Connection = newconnstring
 End With

Debug.Print ActiveWorkbook.Connections(a).ODBCConnection.Connection
ActiveWorkbook.Connections(a).Refresh
DoEvents
Next a
DoEvents
Sheets("Summary").Select
Range("A1").Select
ActiveSheet.PivotTables("SummaryP").PivotCache.Refresh

1 个答案:

答案 0 :(得分:1)

每次刷新前都需要以下内容:  ActiveWorkbook.Connections(a).BackgroundQuery = False