Code execution has been interrupted problem

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Code execution has been interrupted problem

Post by YasserKhalil »

Hello everyone

I have a code that works fine two days ago, but today I got this error message (Code execution has been interrupted)
And it breaks at the line of AutoFill

Code: Select all

        With Sheet2
            .Range("B1").Formula = "=LEN(RC[-1])"
            .Range("B1").AutoFill Destination:=.Range("B1", .Range("A1").End(xlDown).Offset(0, 1))
            .Columns("A:B").Sort Key1:=.Range("B1"), Order1:=xlAscending, Key2:=.Range("A1"), Order2:=xlAscending
            .Columns(2).ClearContents
        End With
The weird thing is that when pressing F8, the code works line by line and with no problem. Any interpretation?

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Code execution has been interrupted problem

Post by YasserKhalil »

I have found the solution at this link
https://stackoverflow.com/questions/215 ... een-halted

But I wonder what is the reason for such an error?

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code execution has been interrupted problem

Post by HansV »

The Visual Basic Editor stores two versions of your VBA code:
- The text that you see in the code modules.
- A semi-compiled version called p-code.
Over time, the p-code can become messy and sometimes even corrupt. The methods that you found in the link that you posted probably force the Visual Basic Editor to clean up the p-code.

I recommend using Rob Bovey's free Excel VBA Code Cleaner. It removes all the p-code, forcing it to be created again from scratch. (It is mentioned in the StackOverflow thread)
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Code execution has been interrupted problem

Post by YasserKhalil »

Thank you very much my tutor.