vba 中断模式下无法执行宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14934433/
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-08 14:59:13  来源:igfitidea点击:

Cannot execute macro in Break Mode

excelexcel-vbaexcel-2010vba

提问by mike

I am trying to write a simple macro to add 1 to the cell's current value:

我正在尝试编写一个简单的宏来将 1 添加到单元格的当前值:

Sub add()
    MsgBox Selection.Value
    Selection.Value = Selection.Value + 1
End Sub

I receive the following error message when I click a (numeric) cell and try to run the macro:

当我单击(数字)单元格并尝试运行宏时,我收到以下错误消息:

Cannot Execute in Break Mode

What am I missing?

我错过了什么?

回答by Peter Albert

You are already executing a macro and somehow stopped its execution (e.g. due to an unhandled error or because you pressed Ctrl-Breakduring the execution). In this state, you cannot execute another macro.

您已经在执行一个宏并以某种方式停止了它的执行(例如,由于未处理的错误或因为您在执行期间按下了Ctrl- Break)。在这种状态下,您不能执行另一个宏。

In the Visual Basic Editor, you need to press the Stop button: enter image description here

在 Visual Basic 编辑器中,您需要按停止按钮: enter image description here

Then you can run the macro.

然后就可以运行宏了。

If you want to understand where the current execution is stopped, right click the code and select Show Next Statement. If you then press F8you can step through the code. F5continues the execution.

如果您想了解当前执行的停止位置,请右键单击代码并选择Show Next Statement。如果您然后按F8您可以单步执行代码。F5继续执行。

回答by Ben Welman

And you should check if the value in the cell is numeric. Example

您应该检查单元格中的值是否为数字。例子

Sub add()
    If IsNumeric(Selection.Value) Then
        Selection.Value = Selection.Value + 1
    Else
        MsgBox ("Not a value selected")
    End If
End Sub

回答by user7357028

Sub Lower()
    Range ("e3"), Value = Range("e3"), Value - 1
End Sub

Sub Higher()
    Range ("e3"), Value = Range("e3"), Value + 1
End Sub