简体   繁体   中英

VBA for excel running Macros

I have a macro running sheets in excel where the data being shown is connected to an external database. I am trying to have the data that is being shown refreshed as the data source is updated, all while the macro is running. To be clear, by "running" I mean the macro is displaying each sheet in the workbook for a number of seconds and just looping.

Here is the code:

Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As             
Long) As Integer 


Public Sub Switch()
Dim ws As Worksheet 

    Do
      For Each ws In ThisWorkbook.Worksheets
        ws.Activate
        Application.Wait Now() + TimeValue("00:00:05")
        If GetAsyncKeyState(vbKeyShift) Then Exit Sub
        DoEvents
      Next ws
    Loop

End Sub

I have tried to insert ActiveWorkbook.RefreshAll just before 'Next ws' and I have also tried ThisWorkbook.RefreshAll in the same location.

As of now, when I am told the data has been updated, I stop the macro from running, which lets the data refresh, then I run the macro again. This is what I am trying to avoid having to do.

Thank you in advance for your help.

Thanks,

Derek

You can turn screen updating on or off during execution of your macro. The macro will run slower with it on.

Application.ScreenUpdating = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM