Excel UserForm.Show "Code execution has been interruped" error

I have a button on a worksheet that when clicked executes:
Private Sub cmdGetQuote_Click()
  frmSelectQuote.Show 
End Sub

Open in new window


The first time the button is clicked, the user form shows.  I have a listbox come up and they click the item they want, then click the OK button, which goes through the list box and returns the selected value to the current worksheet.
Private Sub cmdOK_Click()
    On Error GoTo ErrHandler
    Dim lngItem As Long
    Dim I As Long
    Dim msg As String
 
    If ListBox1.ListIndex <> -1 Then
        For I = 0 To ListBox1.ColumnCount - 1
            msg = msg & ListBox1.Column(I) & vbCrLf
        Next I
    End If

    For lngItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lngItem) Then
            With Sheets("Sheet1") '< qualify sheet here
                .Range("B2").Value = ListBox1.List(lngItem, 0)
            End With
        End If
    Next lngItem
    Unload Me
    
ExitHandler:
  Unload Me
  Exit Sub
    
ErrHandler:
  MsgBox "Error: " & Err.Number & " " & Err.Description & " :ListBox1_DblClick"
  GoTo ExitHandler
  Resume
End Sub

Open in new window


I am then back at Sheet1 and see my value in cell B2.  Then the problem occurs.  I click on the button to show the form again and I get the Microsoft Visual Basic Error dialog box with the error:
Code execution has been interruped

If I click continue, everything is fine.  
Why am I getting this?
How do I prevent this from happening?
maverick0728Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
I can't see anything in the code, can you attach the workbook.
Saurabh Singh TeotiaCommented:
Did you try re-booting your system?? That should take care of the same...
[ fanpages ]IT Services ConsultantCommented:
Hi,

I would advise adding two lines to your code as shown below (suffixed with ' *** ADDED) :

---
Private Sub cmdOK_Click()
    On Error GoTo ErrHandler
    Dim lngItem As Long
    Dim I As Long
    Dim msg As String

  Application.EnableCancelKey = xlDisabled ' *** ADDED
 
    If ListBox1.ListIndex <> -1 Then
        For I = 0 To ListBox1.ColumnCount - 1
            msg = msg & ListBox1.Column(I) & vbCrLf
        Next I
    End If

    For lngItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lngItem) Then
            With Sheets("Sheet1") '< qualify sheet here
                .Range("B2").Value = ListBox1.List(lngItem, 0)
            End With
        End If
    Next lngItem

  Application.EnableCancelKey = xlInterrupt ' *** ADDED

    Unload Me
   
ExitHandler:
  Unload Me
  Exit Sub
   
ErrHandler:
  MsgBox "Error: " & Err.Number & " " & Err.Description & " :ListBox1_DblClick"
  GoTo ExitHandler
  Resume

End Sub
---


This is a recognised "fix" to the error message you have encountered, but comes with a warning:

You will not be able to break ([ CTRL ]+[ BREAK ]) into your code between the two statements as it is executing.

Therefore, please save the code/workbook before attempting re-execution once the lines have been added.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

maverick0728Author Commented:
I used your suggestion, thanks for the help.
I still was getting the error and ended up putting this:
.Range("B2").Activate
before the "end with"

It seems you have to be doing something in the worksheet before you click the button again, or this error continues to come up.

Thanks again for your help.
Jeffrey SmithOwnerCommented:
I just encountered this error today and found this solution:

http://stackoverflow.com/questions/2154699/excel-vba-app-stops-spontaneously-with-message-code-execution-has-been-halted

    Press "Debug" button in the popup.
    Press Ctrl+Pause|Break twice.
    Hit the play button to continue.
    Save the file after completion.
[ fanpages ]IT Services ConsultantCommented:
Thanks for taking the time to post that approach, jeffreywsmith.

I have had mixed results with that in the past, & often found the issue reoccurs; most often within the intended run-time environment when a user cannot use the "Debug" option (due to the VB[A]Project environment being password protected).

Useful to bypass the issue for the short term, though.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.