简体   繁体   中英

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.

The VBA code makes heavy use of object modules, so I was thinking it was a garbage collection issue. 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.

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. 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. 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. Again, the VBA code runs fine, it appears to be a higher level excel issue?

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. 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.

Since it is intermittent, I cannot post the exact excel crash dialog box text.

When a VBA macro button (not AcitveX button) is clicked in rapid succession Excel "sometimes" crashes.

A shot in the dark. Try this. Put your code in lieu of '~~> 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." My hunch was that the ActiveX control is more robust in terms of handling rapid clicks. Turned out to be true.
  2. Added the DoEvents function to the end of the Command Button ActiveX Control event handler. This pretty much eliminated the issue 99.9% unless someone is just being ridiculous clicking the button. 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.

Thanks to all of you who responded with positive comments and suggestions.

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