注意:
在Excel中刷新链接到Access数据库的表
Excel中的表格需要刷新,例如Test_Sheet1,Test_Sheet2,Test_Sheet3
Excel文件由多个用户访问
问题
在Access vba中,如果正在使用excel文件(只读),如何在Access vba代码中实现延迟以等待文件读/写,以便它可以继续使用代码(刷新)表,保存/关闭文件)。请注意,excel文件确实需要按顺序刷新。
我确实实现了一个带延时的Error句柄,所以如果错误号= 1004则延迟X. 这并没有真正做到这一点。
Function RefreshExcelTables()
Dim ExcelApp As Object
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.workbooks.Open "c:\test\Test_Sheet1.xlsb"
ExcelApp.ActiveWorkbook.refreshall
ExcelApp.ActiveWorkbook.Save
ExcelApp.ActiveWindow.Close
ExcelApp.workbooks.Open "c:\test\Test_Sheet2.xlsb"
ExcelApp.ActiveWorkbook.refreshall
ExcelApp.ActiveWorkbook.Save
ExcelApp.ActiveWindow.Close
ExcelApp.workbooks.Open "c:\test\Test_Sheet3.xlsb"
ExcelApp.ActiveWorkbook.refreshall
ExcelApp.ActiveWorkbook.Save
ExcelApp.ActiveWindow.Close
Set ExcelApp = Nothing
End Function
弹出消息(下图)
更新
Function RefreshExcelTables()
On Error GoTo Error
Dim ExcelApp As Object
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.workbooks.Open "c:\test\Test_Sheet1.xlsb"
ExcelApp.ActiveWorkbook.refreshall
ExcelApp.ActiveWorkbook.Save
ExcelApp.ActiveWindow.Close
ExcelApp.workbooks.Open "c:\test\Test_Sheet2.xlsb"
ExcelApp.ActiveWorkbook.refreshall
ExcelApp.ActiveWorkbook.Save
ExcelApp.ActiveWindow.Close
ExcelApp.workbooks.Open "c:\test\Test_Sheet3.xlsb"
ExcelApp.ActiveWorkbook.refreshall
ExcelApp.ActiveWorkbook.Save
ExcelApp.ActiveWindow.Close
Error:
If Err.Number = 1004 Then
call pause(5)
Resume
End If
Set ExcelApp = Nothing
End Function
Public Function Pause(intSeconds As Integer)
Dim dblStart As Double
If intSeconds > 0 Then
dblStart = Timer()
Do While Timer < dblStart + intSeconds
Loop
End If
End Function
答案 0 :(得分:1)
我以前用它来暂停代码处理:
Public Function Pause(intSeconds As Integer)
Dim dblStart As Double
If intSeconds > 0 Then
dblStart = Timer()
Do While Timer < dblStart + intSeconds
' Twiddle thumbs
Loop
End If
End Function
所以你只需要Call Pause(1)
,只要你需要暂停,它就会等待一秒钟。
如果您只需要延迟完整的第二个增量,则效果很好。我有另一个更强大的代码,如果你想要它,可以用更小的增量代码。
答案 1 :(得分:0)
'此代码使用计时器功能暂停运行代码,为午夜(计时器重置为0)作了特殊规定。在MS Access中实现
Public Sub Pause(NumberOfSeconds As Double)
On Error GoTo error_goto
Dim PauseInterval As Variant 'Pause interval is the wait time
Dim StartTime As Variant 'wait start time
Dim ElapsedInterval As Variant 'time elapsed from start time to now
Dim preMidnightInterval As Variant 'time interval from start time to midnight
Dim endTime As Variant
'initializing variables
PauseInterval = NumberOfSeconds
StartTime = Timer
ElapsedInerval = 0
preMidnightInterval = 0
endTime = StartTime + PauseInterval
Do While ElapsedInterval < PauseInterval
ElapsedInterval = Timer - StartTime + preMidnightInterval
'During the day premidnightInterval =0
'shortly after midnight is passed timer is almost 0 and preMidnightInterval becomes non zero
'detecting midnight switch
'the instant midnight is passed ElapsedInterval = 0 - starttime + 0
If ElapsedInterval < 0 Then
preMidnightInterval = 86400 - StartTime 'interval segment before midnight
StartTime = 0 'reset start time to midnight
End If
DoEvents
Loop
'Debug.Print "starttime " & StartTime & "elapsed interval " & ElapsedInterval & " timer:" & Timer & "endtime:" & endTime
Exit_GoTo:
'On Error GoTo 0
Exit Sub
error_goto:
Debug.Print Err.Number, Err.Description, er1
GoTo Exit_GoTo
End Sub