excel vba中的application.ontime方法出错

时间:2015-10-22 12:57:13

标签: excel-vba vba excel

我在excel中有实时数据源,现在想要记录数据,如下所示:

enter image description here]

我在代码上写了如下

Option Explicit

Dim SchedRecalc As Date
Sub Recalc()
    Range("A1").Value = Format(Now, "dd-mmm-yy")
    Range("B1").Value = Format(Time, "hh:mm:ss AM/PM")

    Call SetTime
End Sub

Sub SetTime()
    SchedRecalc = Now + TimeValue("00:00:01")
    Application.OnTime SchedRecalc, "Recalc"
    Application.OnTime SchedRecalc, "Record"

End Sub

Sub Record()
'
' update Macro
'


    With Worksheets("Sheet1")

        Application.ScreenUpdating = False
        Sheet1.Range("A1:A169").Copy _
        Destination:=mySheet.Range("E9")
        Sheet1.Range("E9:E169").Insert Shift:=xlShiftToRight

        Selection.Insert Shift:=xlToRight
        Application.CutCopyMode = False

    End With

End Sub


Sub Disable()
    On Error Resume Next
    Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc",                
    Schedule:=False
    Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Record",    
    Schedule:=False

End Sub

但是出现以下错误..我已禁用所有安全设置:

enter image description here]

1 个答案:

答案 0 :(得分:1)

我成功运行了您的代码。确保使用Application.OnTime引用的过程放在模块

您也可以仔细检查最后一行。复制和粘贴代码并不能使VBA编辑器满意。

Sub Disable()
    On Error Resume Next
    Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc",                
    Schedule:=False
    Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Record",    
    Schedule:=False
End Sub

它应该在一行上。

Sub Disable()
    On Error Resume Next
    Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc", Schedule:=False
    Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Record", Schedule:=False
End Sub