Home » excel » excel – Can not run macro The macro may not be available in this workbook or all marcos may be disabled

excel – Can not run macro The macro may not be available in this workbook or all marcos may be disabled

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to ad a activity (in this example print time) after every 5 second by following code.

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

on running tr1, I am getting following error:

Error screen shot after 5 seconds. Please, let me what am I doing wrong.

How to&Answers:

You have to refer the module as well. This will work, if the code is in Module1:

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

Sub tr2()
    MsgBox "tr2"
End Sub

If it is in a worksheet, the correspondingly:

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

Sub tr2()
    MsgBox "tr2"
End Sub