Consulting

Results 1 to 3 of 3

Thread: Power Point VBA code - Loop

  1. #1
    VBAX Regular
    Joined
    Aug 2019
    Location
    Kuwait
    Posts
    9
    Location

    Power Point VBA code - Loop

    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

  2. #2
    VBAX Regular
    Joined
    Mar 2017
    Posts
    23
    Location
    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.
    Last edited by neilt17; 08-05-2019 at 01:44 AM.

  3. #3
    VBAX Regular
    Joined
    Aug 2019
    Location
    Kuwait
    Posts
    9
    Location
    Excellent, working as required … Thank you neilt17



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •