我们刚刚从excel 2003迁移到2007年,我编写了这段代码来刷新数据连接,然后将结果用于各种位。
问题是,如果我通过按F8它可以运行它,但是当宏运行时,我得到一个关于被保护的单元的错误,即使我的代码包含一个不受保护的宏保护宏。
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
我在ActiveWorkbook.Connections("Connection").Refresh
之后尝试过我们的DoEvents但是没有用。
我也不想真正使用等待,因为有时可能会有10条新线,而其他可能会有数千条。
答案 0 :(得分:1)
更改连接的属性,使其不会在后台刷新。这将迫使代码在继续之前等待刷新。