Search code examples
vbaexcelexcel-2013

VBA excel increments


Hi guys I'm making a simple game in excel and I just need the smiley face image to increment up as its an endless runner type game. I have it moving up once I press the play button but I want it to then continue moving itself. Any ideas ?

Here is the the code I have so far:

Sub Play()

'
'

 Play Macro

'

'
    ActiveSheet.Shapes.Range(Array("Smiley Face 1028")).Select
    Selection.ShapeRange.IncrementTop -20.8333070866

End Sub

Solution

  • You need to move it in a loop with some sort of timer. See this example

    Sub Play()
        Dim shp As Shape
        Dim r As Double
    
        Set shp = ActiveSheet.Shapes("Smiley Face 1028")
        r = shp.Top
    
        Do
            r = shp.Top
            If r <= 1 Then Exit Do
            shp.IncrementTop -1
            Wait 0.5 '<~~ Wait for 30 seconds
        Loop
    
        MsgBox "Animation Over. :) !!!"
    End Sub
    
    Private Sub Wait(ByVal nSec As Double)
        nSec = nSec + Timer
        While nSec > Timer
            DoEvents
        Wend
    End Sub