【发布时间】:2015-08-04 18:34:50
【问题描述】:
我们刚刚从 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 行新行,而其他可能是数千行。
【问题讨论】: