Animation issue with screen updating?

Wookiee213

New Member
Joined
Oct 16, 2014
Messages
34
Hi All

I’m very new to VBA and have run into a problem while adding some simple “animation” (moving images around the screen) to a system I’m developing.


I have a sub which moves a specified image around the screen, I have tested this and it seems to work fine. However when I call it multiple times it seems to stop updating the display/screen with the full animations. But when I insert a msgbox between the Calls then it all works as expected, which leads to me think the issue is probably down to Excel not handling such loops well when changing the display or perhaps there’s a memory issue.

Either way I’d appreciate any advice you can offer on this issue.

Example below (I have a sub which establishes the import of the pictures, their size and start position which is not covered here)


Call One("image one", 400, "UP",)

'MsgBox "pause" ‘with these uncommented it works fine

Call Two("image two", 10, "NONE") ‘this will be displayed at the point the first image reached

'MsgBox "pause" ‘with these uncommented it works fine

Call Three("image three", 400, "DOWNRIGHT") ‘this will start at the point image 2



Code:
Sub movePic(thePic As String, moveIt As Double, picDir As String)
Application.ScreenUpdating = True
 
Dim animLoop As Double
 
Let animLoop = 1
 
For animLoop = 1 To moveIt
    With Workbooks(thisFile).Worksheets("Control").Pictures(thePic)
        Select Case picDir
            Case Is = "LEFT"
                .Left = .Left - 1
            Case Is = "RIGHT"
                .Left = .Left + 1
            Case Is = "UP"
                .Top = .Top - 1
            Case Is = "DOWN"
                .Top = .Top + 1
            Case Is = "DOWNLEFT"
                .Top = .Top + 1
                .Left = .Left - 1
            Case Is = "DOWNRIGHT"
                .Top = .Top + 1
                .Left = .Left + 1
            Case Is = "UPLEFT"
                .Top = .Top - 1
                .Left = .Left - 1
            Case Is = "UPRIGHT"
                .Top = .Top - 1
                .Left = .Left + 1
            Case Is = "NONE"
                .Top = .Top
                .Left = .Left
               
        End Select
       
        '.Top = .Top - 1
        '.Left = .Left + 1
          Application.ScreenUpdating = True
    End With
Next animLoop
   
 
 
Workbooks(thisFile).Worksheets("Control").Pictures(thePic).Delete 'removes it after demo
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Place a DoEvents within your loop.
Rich (BB code):
For animLoop = 1 To moveIt
   DoEvents

Also, do you have a workbook set for the variable thisFile?

This seems to work when the workbook variable is removed:
Rich (BB code):
Option Explicit


Sub test()
   Call movePic("image one", 400, "DOWNRIGHT")
End Sub




Sub movePic(thePic As String, _
            moveIt As Double, _
            picDir As String)


Application.ScreenUpdating = True
 
Dim animLoop As Double
 
Let animLoop = 1
 
For animLoop = 1 To moveIt
   DoEvents
    With Worksheets("Control").Pictures(thePic)
        Select Case picDir
            Case Is = "LEFT"
                .Left = .Left - 1
            Case Is = "RIGHT"
                .Left = .Left + 1
            Case Is = "UP"
                .Top = .Top - 1
            Case Is = "DOWN"
                .Top = .Top + 1
            Case Is = "DOWNLEFT"
                .Top = .Top + 1
                .Left = .Left - 1
            Case Is = "DOWNRIGHT"
                .Top = .Top + 1
                .Left = .Left + 1
            Case Is = "UPLEFT"
                .Top = .Top - 1
                .Left = .Left - 1
            Case Is = "UPRIGHT"
                .Top = .Top - 1
                .Left = .Left + 1
            Case Is = "NONE"
                .Top = .Top
                .Left = .Left
               
        End Select
       
        '.Top = .Top - 1
        '.Left = .Left + 1
          Application.ScreenUpdating = True
    End With
Next animLoop
   
 
'Worksheets("Control").Pictures(thePic).Delete 'removes it after demo
End Sub
 
Upvote 0
bertie that works like a charm! In fact it now works a little too well and I might have to include a delay in the loop somewhere.

Many thanks for the infomation
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,291
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top