Download PowerPoint Virtual Piano Template that plays Musical Notes in...

**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.

```
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
```

## RND Function & Randomize Function in VBA

RND function in Visual Basic Application generates a random number of the data type *Single* which is **less than 1 but greater than or equal to 0.**

Remember to use the **Randomize** Function before using the RND Function. The **Randomize** Function makes the order of the random numbers generated random.

There are many sequences or orders to generate *pseudo-random* numbers via VBA which are called **seeds**.

For any given initial seed, the same number sequence is generated because each successive call to the **Rnd** function uses the previous number as a seed for the next number in the sequence.

Before calling **Rnd,** use the **Randomize** statement without an argument to initialize the random-number generator with a seed based on the system timer.

## Generating a random integer between a given range in VBA

`Int((upperbound - lowerbound + 1) * Rnd + lowerbound) `

Here, *upperbound* is the highest number in the range, and *lowerbound* is the lowest number in the range.

## Consecutive repetitions of being redirected to the same slide

Using the above formula, it is very well likely that the same number is generated again and that we are redirected to the same slide as we are in currently during slide-show mode.

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

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.

**Shuffle Slides Randomly in PowerPoint**

If you want all the slides of a particular range to occur without repetition in slide-show view, you can use the following VBA Macro which literally shuffles the selected slides in a random order.

```
Sub ShuffleSlides()
FirstSlide = 2
LastSlide = 7
Randomize
'generate random number between 2 to 7'
RSN = Int((LastSlide - FirstSlide + 1) * Rnd + FirstSlide)
For i = FirstSlide To LastSlide
ActivePresentation.Slides(i).MoveTo (RSN)
Next i
End Sub
```

**Reverse order of slides (instead of randomising)**

```
Sub ReverseSlideOrder()
For i = 2 To 5 '2 to 5 is the slide range'
ActivePresentation.Slides(5).MoveTo (i)
Next i
'This will reverse the order of slides
the 5th slide will become the 2nd
the 4th slide will become the 3rd...''
End Sub
```

Now if you would like to avoid repetition of slides i.e all slides viewed in a sequence have to be unique, you have to use the following code.

Normally, same random numbers can be generated, we fixed same random numbers being generated consecutively by using the GRN Loops above, however if I am in Slide 2 now and get redirected to Slide 6, I can again still be redirected to Slide 2. To be redirected to unique slides alone, we will use the following code:

# 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
```

## Populate A PowerPoint Quiz From Excel With VBA

Import Questions & Answers of a Microsoft PowerPoint Multiple-Choice Quiz...

## Udayan Gera

Udayan GerA Hey! I am Udayan Gera, 6th grader, Just...

## PowerPoint Educational Game – Free Classroom Chatbot

This PowerPoint Educational Game - Classroom Chatbot can ask questions...