1

I have just moved to excel 2010, have discovered surprising behaviour when stepping through code. When stepping through code it often throws the error Can't execute code in break mode.

An example VBA script follows:

Sub nm()

    Sheets("Winput").Select
    Range("A10").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    ActiveSheet.Previous.Select

end Sub

The error are not thrown on Sheets("Winput").Select or Selection.Copy, but are thrown on all other lines. The code runs fine when i trigger the macro.

thanks in advance

ricardo
  • 8,195
  • 7
  • 47
  • 69

1 Answers1

2

While this may not exactly answer the Can't execute... error, can you please try if the following code throws the same error? I believe that the Select usage in your code is causing this as I've experienced much the same in the past...

Sub nmMod()
    Set Start = Sheets("Winput").Range("A10")
    EndCol = Start.End(xlToRight).Column
    EndRow = Start.End(xlDown).Row
    Start.Resize(EndRow, EndCol).Copy
    Sheet2.Range("A1").PasteSpecial xlPasteAll 'mock action, change as necessary
End Sub

It is best to avoid any kind of selection as much as possible. Emulating a Select action is much slower and gives rise to other issues (ie. They can cancel a standing copy, mess up actions especially involving shapes, etc). It will not be much of a surprise if this is the one causing the error itself.

WGS
  • 13,969
  • 4
  • 48
  • 51
  • +1, thanks. @BK201, this solves the error until the last step, which throws the `Can't execute code in break mode` error once again. Perhaps it's a new `feature` to encourage a _better_ style of code? – ricardo Oct 28 '13 at 09:00
  • `Can't execute...` can trigger if another macro is in break mode. By any chance, do you have any `Worksheet_Change` or `Worksheet_Activate` subroutines in your file? One of them might be causing this macro to throw an error. – WGS Oct 28 '13 at 09:27
  • There are no other macros in the file. Curious. – ricardo Oct 28 '13 at 19:17
  • If you are sure there are no other macros, even in the sheets themselves, then it could be a couple of things: a) You have a breakpoint somewhere in your code; b) A UDF or ActiveX control is messing up your code. It could also be possible that an ActiveX control is calling this macro and it's throwing an error (not entirely sure why); or c) It just goes crazy and you have to use an `On Error Resume Next` before the last line, though this cannot guarantee anything getting copied. – WGS Oct 29 '13 at 03:05
  • It's 2020 and this problem still exists in Excel365. Incredible. – Mikey May 30 '20 at 08:07
  • @Mikey Sadly, a lot of stuff are going to persist until Microsoft ditches VBA and just goes for Python instead. – WGS Jun 01 '20 at 02:18