在单独的计时器上旋转2张图像

时间:2018-03-28 14:23:20

标签: excel vba

我正在尝试在不同计时器上的同一个宏中旋转2个单独的图像。我希望它们同时启动,但以不同的速度旋转。我已经能够让它们一起旋转,但只能以相同的速度旋转。我试图将它们分开,因为它们最多只能一个接一个地运行,或者第二个不能完全旋转

我的代码目前看起来像:

Sub macro()

    rep_count = 0

    Do
    DoEvents

        rep_count = rep_count + 1

        Sheet1.Shapes("Little Clock").Rotation = rep_count
        timeout (0.01)
        Loop Until rep_count = 360

    Do
    DoEvents

        Sheet1.Shapes("Big Clock").Rotation = rep_count
        timeout2 (0.01)
        Loop Until rep_count = 360

End Sub

Sub timeout(duration_ms As Double)
    Start_Time = Timer
    Do
    DoEvents
        Loop Until (Timer - Start_Time) >= 0.1
End Sub

Sub timeout2(duration_ms As Double)
    Start_Time = Timer
    Do
    DoEvents
        Loop Until (Timer - Start_Time) >= 0.7
End Sub

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

普通模块:

Public c As clsTimer

Sub Button1_Click()

    Set c = New clsTimer
    Set c.s1 = ThisWorkbook.Worksheets("Sheet1").Shapes(1)
    Set c.s2 = ThisWorkbook.Worksheets("Sheet1").Shapes(2)
    c.start 1, 2        

End Sub

和一个类模块,clsTimer

Public s1 As Shape
Public s2 As Shape

Public Function start(lngInterval1 As Long, lnginterval2 As Long)

Dim s As Long
Dim lngSecCount As Long

ResetTimer:

s = Timer

Do Until Timer > s + 1
    DoEvents
Loop

lngSecCount = lngSecCount + 1

If lngSecCount Mod lngInterval1 = 0 Then s1.Rotation = s1.Rotation + 1
If lngSecCount Mod lnginterval2 = 0 Then s2.Rotation = s2.Rotation + 1

GoTo ResetTimer

End Function
相关问题