PDA

View Full Version : Power Point VBA code - Loop



mse330
08-03-2019, 03:33 AM
Hi all,

I am working on a presentation & I want to show a number that decreases (like a counter) & after looking on some YouTube videos I think it could be achieved much easier with the VBA loop. However, I am having an issue with the below code where it will start with 10 (in presentation mode) but will not show the numbers 9, 8, 7 … etc. but the screen will freezes until the loop ends & shows me 1 at the end. Any idea what am I doing wrong or how to achieve my goal ?




Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For X = 10 To 1 Step -1
ActivePresentation.Slides(1).Shapes("Rectangle 3").Fill.ForeColor.RGB = RGB((X * 10), (X * 10), (X * 10))
ActivePresentation.Slides(1).Shapes("Rectangle 3").TextFrame2.TextRange.Characters.Text = X
T = Timer
Do While Timer - T < 0.5
Loop
Next
End Sub

neilt17
08-05-2019, 12:09 AM
To get something to show as you do the looping you need to have a call to "DoEvents" - otherwise nothing will happen until all the code has run.

Also X is being passed to that function (as the horizontal position of the mouse pointer), but you are re-using it as something else - so best to use a different name for that variable (doesn't make any difference in this case however):



Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

Dim i As Long
Dim T As Double

For i = 10 To 1 Step -1
ActivePresentation.Slides(1).Shapes("Rectangle 3").Fill.ForeColor.RGB = RGB((i * 10), (i * 10), (i * 10))
ActivePresentation.Slides(1).Shapes("Rectangle 3").TextFrame2.TextRange.Characters.Text = i
T = Timer
Do While Timer - T < 0.5
Loop
DoEvents
Next i
End Sub


Note, however, unless the timer wait value is quite a lot more than 0.5, not every decrement of the counter will be shown in the text box. So this is not a complete solution to your problem.

mse330
08-06-2019, 07:40 AM
Excellent, working as required :thumb … Thank you neilt17