简体   繁体   English

VBA for excel运行宏

[英]VBA for excel running Macros

I have a macro running sheets in excel where the data being shown is connected to an external database. 我在excel中有一个宏运行表,其中显示的数据已连接到外部数据库。 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. 我尝试在“下一个ws”之前插入ActiveWorkbook.RefreshAll,也尝试在同一位置使用ThisWorkbook.RefreshAll。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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