0

So I've set up a custom ribbon that has two buttons: one for running a simulation, and another for cancelling it. When the simulation macro is running, a progress bar appears until it has finished.

The cancel button runs a sub that just has End. But the issue is that when the user clicks cancel, an error comes up that says "Application defined or object defined error"

Basically I'm trying to get a button in the ribbon that stops the simulation macro without throwing an error.

I've tried using an On Error statement to go to the end of the sub, but it still comes up the same. Here is the code for the macro:

Public Sub SimCallback(control As IRibbonControl)

Dim CurrentTrial As Long
Dim NumberOfTrials As Long
Dim StartTime As Double
Dim EndTime As Double
Dim SimBar As ProgressBar
Set SimBar = New ProgressBar

' Custom parameters for progress bar. References separate class
With SimBar
    .Title = "Simulation Progress"
    .StartColour = rgbGreen
    .Top = Application.Top + 125
    .Left = Application.Left + 25
End With

' Pre-sim actions
Worksheets("Histograms").EnableCalculation = False
Worksheets("Monte Carlo Results").EnableCalculation = False
StartTime = Timer
Worksheets("Monte Carlo Calculation").Range("U1:V10000").Clear
Let CurrentTrial = 1
NumberOfTrials = Worksheets("Monte Carlo Results").Range("M2").value
SimBar.TotalActions = NumberOfTrials
SimBar.ShowBar

' Loop repeats until selected number of trials have been performed.
' Values are stored in U and V columns on same sheet.

Do While CurrentTrial < NumberOfTrials
    DoEvents
    Worksheets("Monte Carlo Calculation").Calculate
    For CurrentTrial = 1 To NumberOfTrials
        Worksheets("Monte Carlo Calculation").Range("U" & CurrentTrial).value = Worksheets("Monte Carlo Calculation").Range("J2").value
        Worksheets("Monte Carlo Calculation").Range("V" & CurrentTrial).value = Worksheets("Monte Carlo Calculation").Range("T2").value
        SimBar.NextAction
        Next CurrentTrial
    CurrentTrial = CurrentTrial + 1
Loop

' Output range is copied to separate sheet, since referencing the original sheet slowed down the simulation time.

Worksheets("Monte Carlo Calculation").Range("U1:V10000").Copy Destination:=Worksheets("Histograms").Range("H1:J10000")
Worksheets("Histograms").EnableCalculation = True
Worksheets("Monte Carlo Results").EnableCalculation = True

' Display sim time and terminate progress bar
EndTime = Round(Timer - StartTime, 2)
SimBar.Terminate
MsgBox "Simulation Complete (" & EndTime & " seconds)", vbInformation

End Sub
1

1 Answer 1

0

On possibility is as follows:

1) Create a public variable called e.g. Running:

Public Running As Boolean

declared in a standard code module outside of any procedure.

2) In the code for SimCallback, have the line

Running = True

towards the top of the code and replace

Do While CurrentTrial < NumberOfTrials

by

Do While Running And CurrentTrial < NumberOfTrials

3) In the code for the cancel button, replace End by

Running = False

This will allow the cleanup line SimBar.Terminate to execute.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.