VBA Macro running too fast


江枫思渺然 提交于 2020-01-17 16:39:27

问题


It's weird that I'm finding ways to slow down my macro. Apart from Doevents and other time delay techniques, which are basically a workaround, is there a way through which we can get around the asynchronous execution. As in, I want the VBA code to behave like this:

start executing line 1>finish executing line 1>move to line 2;

Forgive if I'm wrong but currently it seems to follow:

Start executing line 1>without caring whether line 1 finished or not start executing line2

回答1:


If you are calling external programs (vbs, exe) then the vba isn't getting any feedback on the process at all. It calls the programs and moves on to the next line of code (the vba doesn't know if/when the external programs finishes). One way to slow this process down would be to put a application.wait or application.sleep between the calls, but that is also a workaround. Please post your actual code and perhaps we can troubleshoot further.




回答2:


if the code is about refreshing data, use Refresh method with backgroundquery=False in a For Loop instead of RefreshAll.

For Each con In Me.Connections
    con.ODBCConnection.BackgroundQuery = False
    con.Refresh
Next


来源:https://stackoverflow.com/questions/46117587/vba-macro-running-too-fast

工具导航Map