无法运行宏该宏可能在此工作簿中不可用,或者可能禁用了所有marcos

时间:2018-06-22 09:28:48

标签: excel vba excel-vba

我尝试每5秒通过以下代码对一项活动(在本示例中为打印时间)进行广告。

Sub tr1()
dim i as Integer
i = Range("b1").Value
If i < 3 Then
Application.OnTime Now + TimeValue("00:00:05"), "tr2", , True
End If

Range("a" & i).Value = UCase(Format(Now, "HH:MM:SS"))
Range("b1").Value = Range("b1").Value + 1
MsgBox ("tr1 called")
End Sub

Sub tr2()
Application.OnTime Now + TimeValue("00:00:05"), "tr1"
MsgBox ("tr2 called")
End Sub

在运行tr1时,出现以下错误:

Error screen shot 5秒后。拜托,让我做错什么。

1 个答案:

答案 0 :(得分:2)

您还必须引用该模块。如果代码在Module1中,则可以使用此方法:

Sub tr1()
    Application.OnTime Now + TimeValue("00:00:01"), "!Module1.tr2", , True
End Sub

Sub tr2()
    MsgBox "tr2"
End Sub

如果在工作表中,则相应地:

Sub tr1()
    Application.OnTime Now + TimeValue("00:00:01"), "!Sheet1.tr2", , True
End Sub

Sub tr2()
    MsgBox "tr2"
End Sub