简体   繁体   English

多次运行 Excel VBA OnTime 宏

[英]Excel VBA OnTime Macro running multiple times

I have a simple code that will copy the current value of my stock portfolio and paste it into a table.我有一个简单的代码,可以复制我的股票投资组合的当前价值并将其粘贴到表格中。 I recently tried to add in a timer so that it did this automatically every hour.我最近尝试添加一个计时器,以便它每小时自动执行一次。 It works, however, it pastes the value four or five times into the table.但是,它可以将值粘贴四到五次到表中。 I searched around and I think this might be because it takes only a split second to run the macro and so it loops until a full second has passed.我四处搜索,我认为这可能是因为运行宏只需要一秒钟,因此它会循环直到整整一秒过去。

Here's the code:这是代码:

Sub AutoPL()
    Worksheets("Overview").Range("C15:D15").Copy
    Worksheets("Data").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial                     xlPasteValues
    Application.CutCopyMode = False
    Call PLTimer
End Sub


Sub PLTimer()
    Application.OnTime Now + TimeValue("01:00:00"), "AutoPL"
End Sub

@mclapham 在再次调用Application.OnTime之前,我通过使用Application.Wait Now + TimeValue("00:00:01")解决了您在评论中描述的冻结。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM