我有一个简单的宏和一个开始运行它的按钮。我还想有一个按钮来停止它,所以我不必打开 vba 编辑器来执行它。
这是代码。
Sub Data_Recording()
With Workbooks("Chart_Macro").Sheets("Chart")
.Rows(5).EntireRow.Insert Shift:=xlDown,
CopyOrigin:=xlFormatFromLeftOrAbove
.Range("B5:F5").Value = .Range("B2:F2").Value
End With
Application.OnTime Now + TimeValue("00:00:20"), "Data_Recording"
End Sub
非常感谢。
最佳答案
就像 Armando 建议的那样,您可以使用以下代码:
Sub Data_Recording()
With Workbooks("Chart_Macro").Sheets("Chart")
If .Range(CHECK_ADDRESS) = True Then Exit Sub
.Rows(5).EntireRow.Insert Shift:=xlDown,
CopyOrigin:=xlFormatFromLeftOrAbove
.Range("B5:F5").Value = .Range("B2:F2").Value
End With
Application.OnTime Now + TimeValue("00:00:20"), "Data_Recording"
End Sub
Public Sub SetRecordingState(bStop As Boolean)
Workbooks("Chart_Macro").Sheets("Chart").Range(CHECK_ADDRESS) = bStop
End Sub
因此,我在您的代码中添加了一行来检查值。您可以使用另一个子更改状态。如果 State 设置为 True,程序将停止
关于vba - 如何在 excel-vba 中添加 'Stop Macro'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32770790/