简体   繁体   English

在快速连续中单击宏按钮时,Excel会间歇性地崩溃

[英]Excel Crashes Intermittently When Clicking Macro Button in Rapid Succession

When a VBA macro button (not AcitveX button) is clicked in rapid succession Excel "sometimes" crashes. 当快速连续点击VBA宏按钮(不是AcitveX按钮)时,Excel“有时”会崩溃。

The VBA code makes heavy use of object modules, so I was thinking it was a garbage collection issue. VBA代码大量使用对象模块,所以我认为这是垃圾收集问题。 I explicitly set the top level object to nothing before exiting the button click macro thinking it would force a garbage collection. 在退出按钮单击宏之前,我明确地将顶级对象设置为空,以为它会强制进行垃圾回收。 That did not work. 那没用。

It is super frustrating because it is intermittent. 这是非常令人沮丧的,因为它是间歇性的。 Maybe 1 out of 10 to 20 times. 可能是10到20次中的1次。

The code shown is just the button click handler. 显示的代码只是按钮单击处理程序。 There is about 10,000 lines of code called from this handler, which I did not show. 从这个处理程序调用大约10,000行代码,我没有显示。 The VBA code reads information from the sheet, does some calculations, updates an excel chart on the sheet, and writes some data back to the worksheet. VBA代码从工作表中读取信息,进行一些计算,更新工作表上的Excel图表,并将一些数据写回工作表。 I do the usual turning off events and screen updates. 我通常关闭事件和屏幕更新。

I am just hoping someone else has come up against the rapid macro execution causing excel to crash. 我只是希望别人遇到快速宏执行导致excel崩溃。 Again, the VBA code runs fine, it appears to be a higher level excel issue? 再一次,VBA代码运行良好,它似乎是一个更高级别的Excel问题?

Public Sub Clicked_UpdateWall_C()
    Dim Wall As New CWall_C
    Dim ExecutionSuccess As Boolean
    Dim errstr As String

    ExecutionSuccess = CheckUnits(ActiveSheet.Name, errstr)

    If ExecutionSuccess Then ExecutionSuccess = Wall.UpdateWall(ActiveSheet.Name, errstr)

    Call CheckError(ExecutionSuccess, errstr)

    ' This is an attempt to force excel to do garbage collection
    Set Wall = Nothing
End Sub

The error message is "Excel has stopped working" not a VBA runtime error. 错误消息是“Excel已停止工作”而不是VBA运行时错误。 One can click the "restart excel" button in the error dialog, and excel restarts and generally most of the time one does not lose work. 可以单击错误对话框中的“重启excel”按钮,然后重启,通常大部分时间都不会丢失工作。

Since it is intermittent, I cannot post the exact excel crash dialog box text. 由于它是间歇性的,我无法发布确切的excel崩溃对话框文本。

When a VBA macro button (not AcitveX button) is clicked in rapid succession Excel "sometimes" crashes. 当快速连续点击VBA宏按钮(不是AcitveX按钮)时,Excel“有时”会崩溃。

A shot in the dark. 盲目猜测。 Try this. 尝试这个。 Put your code in lieu of '~~> Rest of your code . 用你的代码代替'~~> Rest of your code Now no matter how many times you click in succession, nothing will happen. 现在无论你连续点击多少次,都不会发生任何事情。

Option Explicit

Dim DoNotRunProc As Boolean

Public Sub Clicked_UpdateWall_C()
    If DoNotRunProc = True Then Exit Sub

    DoNotRunProc = True

    On Error GoTo Whoa

    '
    '~~> Rest of your code
    '

Whoa:
    DoNotRunProc = False
End Sub

Note: If you have a separate error handler in your code then adjust the above code accordingly. 注意:如果代码中有单独的错误处理程序,则相应地调整上面的代码。

I was able to resolve the issue by doing two things: 我能够通过做两件事来解决这个问题:

  1. Switched from a "Button Form Control" to an "Command Button ActiveX Control." 从“按钮窗体控件”切换到“命令按钮ActiveX控件”。 My hunch was that the ActiveX control is more robust in terms of handling rapid clicks. 我的预感是ActiveX控件在处理快速点击方面更加强大。 Turned out to be true. 原来是真的。
  2. Added the DoEvents function to the end of the Command Button ActiveX Control event handler. 将DoEvents函数添加到Command Button ActiveX控件事件处理程序的末尾。 This pretty much eliminated the issue 99.9% unless someone is just being ridiculous clicking the button. 这几乎消除了99.9%的问题,除非有人只是单击按钮是荒谬的。 The hunch here is that it gave Excel time to handle any outstanding events that perhaps were not handled properly due to rapid button clicks. 这里的预感是它给了Excel时间来处理由于快速按钮点击而可能无法正确处理的任何未完成事件。

Thanks to all of you who responded with positive comments and suggestions. 感谢所有回复了积极评论和建议的人。

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

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