Vba freeze during stepping through for loop bit of code.

romanemul

New Member
Joined
Jul 22, 2016
Messages
35
Hi members.

I try to rotate a shape with this bit of vba thus creating an animation.

Code is in Module

Code:
Public Declare Function Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub rectanim()


Dim wb1 As Workbook
Dim ws1 As Worksheet

Set wb1 = Workbooks("anim.xlsm")
Set ws1 = wb1.Worksheets("Sheet1")

Dim d As Shape

Set d = ws1.Shapes("Rec1")


d.Fill.ForeColor.RGB = RGB(0, 0, 0)
d.Select

For i = 0 To 359

    DoEvents    
       Sleep 20
       d.Rotation = i

Next i

End Sub

code hangs at Sleep 20. I know that excel can run into unlimited loop when using unappropriate code. However this freezes during stepping through the code.

Anyone ?

Thanks
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this instead

Code:
Sub rectanim()




Dim wb1 As Workbook
Dim ws1 As Worksheet


Set wb1 = Workbooks("anim.xlsm")
Set ws1 = wb1.Worksheets("Sheet1")


Dim d As Shape


'Set d = ws1.Shapes("Rec1")
 Set d = ws1.Shapes("Rectangle 1")




d.Fill.ForeColor.RGB = RGB(0, 0, 0)
d.Select


For i = 0 To 359


    DoEvents
        Application.Wait (Now + (0.0000002))
        'Sleep 20
       d.Rotation = i


Next i


End Sub
 
Upvote 0
Yep this works :)
Thanks

I didnt use Wait function because i find syntax quite messy. Nothing is cheaper than sleep + somevalue and i used sleep function previously without any issues.
Any clue why sleep function becomes unresponsible ?
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,930
Members
449,479
Latest member
nana abanyin

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