excel 2010 VBA 在步进时抛出“无法在中断模式下执行代码”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19624411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:05:38  来源:igfitidea点击:

excel 2010 VBA throws "can't execute code in break mode" while stepping

excelvbaexcel-vba

提问by ricardo

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.

我刚刚转到 excel 2010,在单步执行代码时发现了令人惊讶的行为。在单步执行代码时,它通常会抛出错误Can't execute code in break mode

An example VBAscript follows:

示例VBA脚本如下:

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").Selector Selection.Copy, but are thrown on all other lines. The code runs fine when i trigger the macro.

错误不会在Sheets("Winput").SelectSelection.Copy上引发,而是在所有其他行上引发。当我触发宏时,代码运行良好。

thanks in advance

提前致谢

回答by Jerome Montino

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 Selectusage in your code is causing this as I've experienced much the same in the past...

虽然这可能不能完全回答Can't execute...错误,但您能否尝试以下代码是否抛出相同的错误?我相信Select您的代码中的使用导致了这种情况,因为我过去经历过很多相同的事情......

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 Selectaction 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.

最好尽可能避免任何类型的选择。模拟Select动作要慢得多,并会引起其他问题(即,它们可以取消站立copy,弄乱动作,尤其是涉及形状等)。如果这是导致错误本身的原因,那就不足为奇了。