简体   繁体   中英

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")解决了您在评论中描述的冻结。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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