VBA: Code execution has been interrupted

Skovgaard

Board Regular
Joined
Oct 18, 2013
Messages
197
Office Version
  1. 365
Platform
  1. Windows
Hi,

Please see my code below. It works fine when running.
Hope someone can help, I'm still a newbie with VBA.

1. Every time I close my workbook, I get the message: "Code execution has been interrupted". Why?
2. Can my code be written smarter/shorter?



Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Target.Cells.Count > 1 Then Exit Sub
    If Range("N30").Value = 0 Then Exit Sub
    
    If Not Intersect(Target, Range("D8:D9")) Is Nothing Then
        If Target.Value = "" Then
            MsgBox "Projektnavn og VVS'er skal være udfyldt, hvis projektrabat gives." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at slette feltet"
            Application.Undo
        End If
    End If


    If Not Intersect(Target, Range("E14:L27")) Is Nothing Then
        If Range("O28").Value < Range("N8").Value And Range("E28").Value < Range("N9").Value Then
            MsgBox "Kriterie for projektrabat ikke opfyldt." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at ændre feltet"
            Application.Undo
        End If
    End If
      
    If Not Intersect(Target, Range("E14:L27")) Is Nothing Then
        If Range("AI28").Value < 0.3 And Range("N30").Value > 0 Then
            MsgBox "Kriterie for projektrabat ikke opfyldt." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at ændre feltet"
            Application.Undo
        End If
    End If
    
    
    If Not Intersect(Target, Range("E14:L27")) Is Nothing Then
        If Range("AI28").Value <= 0.4 And Range("N30").Value > 0.05 Then
            MsgBox "Kriterie for projektrabat ikke opfyldt." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at ændre feltet"
            Application.Undo
        End If
    End If
    
    


End Sub


Best Regards
Skovgaard
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
The error you are getting can occur only if code is running. It normally should only happen if you force the code to stop with CTRL-BREAK but there are cases where it does so spontaneously. This code won't run when you close your workbook, unless there is other code that changes the worksheet. What other code do you have? Do you have code in the ThisWorkbook module?

Also, there is overlapping logic in your code above that can be simplified. This is not causing your problem but you may find this suggestion useful:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub
    If Range("N30").Value = 0 Then Exit Sub
    
    If Not Intersect(Target, Range("D8:D9")) Is Nothing Then

        If Target.Value = "" Then
            MsgBox "Projektnavn og VVS'er skal være udfyldt, hvis projektrabat gives." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at slette feltet"
            Application.Undo
        End If

    ElseIf Not Intersect(Target, Range("E14:L27")) Is Nothing Then

        If Range("O28").Value < Range("N8").Value And Range("E28").Value < Range("N9").Value Then
            MsgBox "Kriterie for projektrabat ikke opfyldt." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at ændre feltet"
            Application.Undo
        End If
      
        If Range("AI28").Value < 0.3 And Range("N30").Value > 0 Then
            MsgBox "Kriterie for projektrabat ikke opfyldt." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at ændre feltet"
            Application.Undo
        End If

        If Range("AI28").Value <= 0.4 And Range("N30").Value > 0.05 Then
            MsgBox "Kriterie for projektrabat ikke opfyldt." & vbNewLine & vbNewLine & "Sæt projektrabat = 0%, for at ændre feltet"
            Application.Undo
        End If

    End If
    
End Sub
 
Upvote 0
Thanks for your reply!
I have no other code, this is the only one.

I've tried to simplify my code, with your example. So far with a positive result. I haven't got the error yet, so hopfully this will do the trick.

Thanks once again.

/Skovgaard
 
Upvote 0
I'm replying to this message because a co-worker has this same issue and I can't seem to find a sultion for it... Everytime that he closes any excel file, either with or without macros, with or without data he gets the "Code Execution has been interrupted" and the debug button is grayed out, so I can't do the CTRL+break solution... Does anyone has any idea why this pop up is showing up?
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top