Lesson 25: Creating Animation in Excel VBA


Beside creating VBA  code for mathematical and financial calculations, it is also possible to creating some fun applications in Excel VBA, including games and animation. Although professionals programmers might not be interested to write such applications, it is worth while trying them out as a hobby and for personal satisfaction.

The code:

Private Sub StartButton_Click()

repeat:


With VBAProject.Sheet1.Image1
.Left = .Left + 1
DoEvents
If .Left > 200 Then .Left = 1
End With

GoTo repeat


End Sub


If you wish to move the object up and down, change the above code by replacing the property Left to Top, the code appear as follows:

Private Sub StartButton_Click()

repeat:


With VBAProject.Sheet1.Image1
.Top= .Top+ 1
DoEvents
If .Top> 200 Then .Top = 1
End With

GoTo repeat


End Sub



 

If you wish to make the object move diagonally, then use the properties Top and Left at the same time, as follows:

Private Sub StartButton_Click()
repeat:

With VBAProject.Sheet1.Image1

.Top = .Top + 5
.Left = .Left + 5

DoEvents
If .Top > 200 Then .Top = 1
If .Left > 200 Then .Left = 1


End With

GoTo repeat
End Sub
 




Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page