Excel 2007数据刷新,用于与工作表保护

我们刚刚从Excel 2003移到了2007,我编写了这个代码来刷新数据连接,然后使用各种位的结果。
问题是,如果我通过按F8运行它,它的工作原理,但是当macros本身运行,我得到一个有关被保护的单元格的错误,即使我的代码包含一个不受保护和保护macros。

Sub code() UnPro 'Macro to Unprotected worksheet Dim p As Integer Dim q As Integer Dim lastnew As Long Dim LastOld As Long Dim pctCompl As Single Dim x As Integer Dim y As Integer pctCompl = 5 'Update Progress Bar progress pctCompl 'Update Progress Bar 'Range("A6").QueryTable.Refresh BackgroundQuery:=False ActiveWorkbook.Connections("Connection").Refresh pctCompl = 10 'Update Progress Bar progress pctCompl 'Update Progress Bar p = 0 q = 0 Do Until q = 1 If Sheets("Main").Range("G4").Offset(p, 0).Text <> "" Then p = p + 1 Else q = 1 lastnew = p + 3 End If Loop x = 0 y = 0 pctCompl = 50 'Update Progress Bar progress pctCompl 'Update Progress Bar Do Until y = 1 If Sheets("Main").Range("H4").Offset(x, 0).Value <> "" Then x = x + 1 Else y = 1 LastOld = x + 3 End If Loop pctCompl = 70 'Update Progress Bar progress pctCompl 'Update Progress Bar If LastOld <> lastnew Then Range(Cells(LastOld - 1, 8), Cells(LastOld - 1, 8)).Select Selection.AutoFill Destination:=Range(Cells(LastOld - 1, 8), Cells(lastnew, 8)), Type:=xlFillDefault End If pctCompl = 100 'Update Progress Bar progress pctCompl 'Update Progress Bar ProgBar.Hide ProTe 'Macro to Protected worksheet End Sub 

我已经尝试过我们DoEvents ActiveWorkbook.Connections("Connection").Refresh但没有奏效。 我也不想用一个等待,因为有时可能会有10个新的线,其他的可能是数千个。

更改连接的属性,使其不会在后台刷新。 这会迫使代码在刷新之前等待,然后再继续。