Click here to obtain the VBA Code that would automatically & randomly shuffle and reorder your PowerPoint Presentation Slides.

How to jump to a RANDOM SLIDE in PowerPoint using VBA

In this module tutorial, we shall be seeing how we can be redirected to a random slide in PowerPoint using Visual Basic Code.

In our module we have six different slides containing unique names of students from A to F. I need to be sent to any one of those slides randomly on the press of a button.

I also need to make sure that I am not being redirected to the same slide containing the same student name consecutively in the case that the same random number is again generated.

So now, let us shuffle our slides and get hyperlinked to a random slide in Microsoft PowerPoint.

Consecutive repetitions of being redirected to the same slide

While Using ((Upperbound – Lowerbound + 1) * Rnd + Lowerbound), it is likely that the same number is generated again causing us to be redirected to the same slide that we are currently viewing.

If RSN = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex Then
GoTo GRN
End If 

To not allow this repetition to occur while jumping to random slides in PowerPoint, we add a If-Then Condition which states that the random number has to be again generated if it is equal to the current slide number of the presentation which is being shown in the slide-show.

We redirect the code back to GRN and a loop is formed. A fair warning: Do not put too many GoTo Redirects as it can cause your codes to become a prime example of a spaghetti code.

However since we can keep an eye out here based on the fact that there is only a difference of 2 lines between our GoTo Statement and GRN snippet, I am not heavily concerned.

Sub GotoRandomSlide()
FirstSlide = 2
LastSlide = 7

GRN:
'generate a random no between first slide and last slide'
RSN = Int((LastSlide - FirstSlide + 1) * Rnd + FirstSlide)
'loop so that the same number is not generated consecutively'
If RSN = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex Then GoTo GRN

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

GOING THROUGH ALL SLIDES RANDOMLY WITH NO REPETITION

Public Position
Public Range
Public AllSlides() As Integer

Sub ShuffleAndBegin()

'Declare Slides'
FirstSlide = 3
LastSlide = 8
Range = LastSlide - FirstSlide
ReDim AllSlides(Range) 'Number of Pockets'

'Adding SlideNumbers to the Pockets'
For i = 0 To Range
AllSlides(i) = FirstSlide + i
Next

Randomize

For N = 0 To Range
J = Int((Range + 1) * Rnd) 'Random number between 0 To Range'

If N <> J Then
temp = AllSlides(N)
AllSlides(N) = AllSlides(J)
AllSlides(J) = temp
End If

Next N

Position = -1
End Sub 
Sub Advance()
Position = Position + 1

If Position > Range Then
'what to do when all slides in array is shown'
ActivePresentation.SlideShowWindow.View.GotoSlide (9)
End If

ActivePresentation.SlideShowWindow.View.GotoSlide AllSlides(Position)
End Sub 

Jumping to slide based on weighted probability

If you wanted to jump to a random slide based on some form of probability, for example:

  • 50% chance of jumping to Slide 2 randomly
  • 10% for Slide 3
  • 30% for Slide 4
  • 10% for Slide 5.

We would use the following code containing the If-Then Condition based on the value of Rnd. Do note that Rnd generates a number from 0 to 1.

If Rnd > 0.5 Then
SlideShowWindows(1).View.GotoSlide 2
ElseIf Rnd < 0.5 And Rnd > 0.4 Then
SlideShowWindows(1).View.GotoSlide 3
ElseIf Rnd < 0.4 And Rnd > 0.1 Then
SlideShowWindows(1).View.GotoSlide 4
ElseIf Rnd < 0.1 Then
SlideShowWindows(1).View.GotoSlide 5
End If