PowerPoint – Creating a Random Slideshow Quiz – Part 2

Generate a non-repeating random number

Picking up where we left off in part one, this second part will show how we can extend the VBA to generate non-repeating numbers. As it defeats the object a little to have a quiz that repeats questions!

The code so far looks like this:

Sub rndSlide()
    Dim actSlide As Integer

    Randomize
    actSlide = Int(9 * Rnd) + 2)

    ActivePresentation.SlideShowWindow.View.GotoSlide (actSlide)
End Sub

If we want to ensure that a question slide does not repeat, we need to store the value for the question slide somewhere and then check against it with subsequent button presses.

To do this we need to create an array, as the array will need to be used outside as well as inside our Subroutine, we need to declare it as a Global variable and we need a function that tests the existence of the value in the array, so the code will now look like this:

Option Explicit
Public usedSlides(10) As Variant
Public i As Integer

Sub rndSlide()
    Dim actSlide As Integer
    Dim inArray As Boolean
    
    Randomize
    actSlide = Int(9 * Rnd) + 2

    inArray = CheckArray(actSlide, usedSlides)
    
    Do Until inArray = False
        actSlide = Int((UBound(usedSlides) * Rnd) + 2)
        inArray = CheckArray(actSlide, usedSlides)
    Loop

    ActivePresentation.SlideShowWindow.View.GotoSlide (actSlide)

    usedSlides(i) = actSlide
    i = i + 1
End Sub

Private Function CheckArray(rndNum As Variant, arrSlides As Variant) As Boolean
Dim slideNum As Variant
On Error GoTo CheckArrayError: 'array is empty
    For Each slideNum In arrSlides
        If slideNum = rndNum Then
            CheckArray = True
            Exit Function
        End If
    Next slideNum
Exit Function
CheckArrayError:
On Error GoTo 0
CheckArray = False
End Function

Rather than reinvent the wheel, I will only breakdown the additions rather than the entire script. The first thing to note are the additions of the Global declarations for the array and each array element:

Option Explicit
Public usedSlides(10) As Variant
Public i As Integer

We now go ahead and check if the random number created and added to the variable actSlide is in the array:

inArray = CheckArray(actSlide, usedSlides)

The line above calls the Function CheckArray passing it the slide number we want checked(actSlide) against the array(usedSlides):

Private Function CheckArray(rndNum As Variant, arrSlides As Variant) As Boolean
Dim slideNum As Variant
On Error GoTo CheckArrayError: 'array is empty
    For Each slideNum In arrSlides
        If slideNum = rndNum Then
            CheckArray = True
            Exit Function
        End If
    Next slideNum
Exit Function
CheckArrayError:
On Error GoTo 0
CheckArray = False
End Function

CheckArray will return a boolean(true or false) response so the following line will keep checking the array until the existence of actSlide returns false:

    Do Until inArray = False
        actSlide = Int((UBound(usedSlides) * Rnd) + 2)
        inArray = CheckArray(actSlide, usedSlides)
    Loop

When it eventually returns true, the code will move to the appropriate slide and execute the final additions. First we need to add the slide number to the array usedSlides:

    usedSlides(i) = actSlide

The variable i representing the value for the position in the array. Finally we need to increment the positional value in the array, or we will end up adding each iteration of actSlide to the same element of the array:

    i = i + 1

As with our first, very simple, randomiser script. There are a couple of limitations:

  • We are having to manually set the size of the array to the number of slides to be used for questions in the slideshow;
  • We have no way of identifying when the game is over, i.e. all slides have been used;
  • We have not created a way to start all over again;
  • We don’t have a method of scoring or timing each round.

These questions and possibly others will be answered in the subsequent parts of this tutorial, so please….Read on…..