vba - 无法运行宏 该工作簿中的宏可能不可用,或者所有宏都可能被禁用

标签 vba excel

我试图通过以下代码每 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 秒后。请让我告诉我我做错了什么。

最佳答案

您还必须引用该模块。如果代码在 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

关于vba - 无法运行宏 该工作簿中的宏可能不可用,或者所有宏都可能被禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50984883/

相关文章:

excel - 如何更改指定列中的单元格颜色?

excel - 从单元格中删除所有星号的宏

Excel 将表格顺时针旋转(不是转置)90 度

html - VBA 数据从 Google 导入 Excel : Custom Time Ranges

excel - 使用excel公式删除多个子字符串的匹配字符串

excel - 在工作表上进行更改时使 VBA 函数自动更新 - excel

vba - excel日期格式与vba中的不一样

vba - 我的代码生成一个无限循环

excel - 如何在 MAC 上使用 VBA 从 Excel 16 打开工作簿?

.net - 更新 Excel 文件中的小计