Rectangle racing with Excel VBA


Here is the handout from early September of 2001


****** Visual Basic for Applications (VBA) program for Microsoft Excel *******
                                      ---
Dim delayAmt                   <------------This is a GLOBAL variable

Sub raceEm()

    Dim delayAfterEach
    Dim notDone
    
    If ActiveSheet.Rectangles.Count = 0 Then
        MsgBox "You need to use a worksheet that has some rectangles!"
        Exit Sub
    End If
    
    delayAfterEach = InputBox("Delay after every move? 0 for no, 1 for yes")
    
    delayAmt = InputBox("Delay (seconds) you want applied?  0, 0.1, 0.5, 1?")
    
    notDone = 1
    
    Do While notDone = 1

        For Each Rect In ActiveSheet.Rectangles

            Rect.Top = Rect.Top + (Rnd() * 10)
            
            If delayAfterEach = 1 Then
                pauseDelayAmt
            End If
            
            If Rect.Top > 150 Then
                notDone = 0
            End If

        Next Rect
        
        If delayAfterEach = 0 Then
            pauseDelayAmt
        End If
    Loop
    
    MsgBox "Thanks for using Panther software products!"

End Sub

Sub pauseDelayAmt()
    
    stopTime = Timer + delayAmt
    
    theTime = Timer

    Do While theTime < stopTime
        theTime = Timer
    Loop

End Sub   

    *** From 810:022 Microcomputer Applications and Systems Integration
        VBA is a subset of Visual Basic - It is part of Word 97, Excel 97,
        Access 97 and PowerPoint 97 (Microsoft Office).