2017-09-14 77 views
1

我正在試圖創建一個計數器,該計數器在Powerpoint演示文稿的VBA中每秒增加1。這是我到目前爲止所提出的。如何在VBA中創建遞增計時器

Sub countup() 
    Dim index As Integer 

    index = 0 
    Do Until index > 100 
    index = index + 1 

    DoEvents 
     With ActivePresentation.Slides(1).Shapes(3).TextFrame.TextRange 
     .Text = index 
     End With 

    Loop 
End Sub 

此代碼增加1直到101,但它不會每秒增加1。這是在VBA的Powerpoint,所以我不能把一個計時器控制。希望你能幫忙。謝謝。

回答

3

這是一個可能的解決方案:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

Public Sub TestMe2() 

    Dim index As Long 
    index = 0 

    Do Until index > 10 
     index = index + 1 
     Debug.Print index 
     Sleep (1000) 
    Loop 

End Sub 

無需添加.Net功能,它可以做到這樣的:

Public Sub TestMe() 

    Dim lngIndex   As Long 
    Dim sngSec    As Single '9GAG 
    Dim sngAddSec   As Single 

    sngAddSec = 1 

    Do Until lngIndex > 4 
     lngIndex = lngIndex + 1 
     sngSec = Timer + sngAddSec 
     Debug.Print lngIndex 
     While Timer < sngSec: Wend 
    Loop 

End Sub