David Marcovitz's Randomizing Slides Code

T

TeacherValerie

I am a teacher created a game for my students I plan to use next week....

I found a post from David and am trying to use it but it is not working. I
want to randomize slides 2-26 in my presentation. So I created a button on
the first page that links to the GetStarted macro and a button on the 2nd
page that links to the GoToRandom macro.

When I added it, it automatically created 3 macros for me (the 3rd being
Initialize). Can you tell me if there is a mistake in my code (see below)?

THANKS!!!!!!!!!!!!!!!!!
Valerie

Sub GetStarted()
Dim doneSlide(6) As Boolean

Sub GetStarted()
Initialize
GoToRandom
End Sub

Sub Initialize()
Dim i As Long

For i = 2 To 26
doneSlide(i) = False
Next i
End Sub

Sub GoToRandom()
Dim nextSlide As Long

Randomize
If doneSlide(2) And doneSlide(3) And doneSlide(26) And doneSlide(5) Then
ActivePresentation.SlideShowWindow.View.GotoSlide 6
Else
nextSlide = Int(Rnd * 26) + 2
While doneSlide(nextSlide)
nextSlide = Int(Rnd * 26) + 2
Wend
doneSlide(nextSlide) = True
ActivePresentation.SlideShowWindow.View.GotoSlide nextSlide
End If
End Sub

Just set the button on your first slide to GetStarted and a button on slides
2, 3, 4, and 5 to GoToRandom
End Sub
 
P

Preschool Mike

Try one of these codes below. I use them in my games and quizes and they
work really well. All your slides between the first and the last in your
game will be random. You'll also be able to add more slides between the
first two if you wish without changing any of the code. You might also try
David's web site www.powerfulpowerpoint.com. Lot's of examples from his book
and from other people.

Use this one if you want to be able to randomly go back to a slide already
visited.
Dim numSlides As Long
Dim numRead As Integer
Dim numWanted As Integer
Dim visited() As Boolean

Sub GetStarted() 'Set a button on your start page to run this code.
Initialize
RandomNext
End Sub

Sub Initialize()
Randomize
numWanted = ActivePresentation.Slides.Count
numRead = 0
numSlides = ActivePresentation.Slides.Count
ReDim visited(numSlides)
For i = 2 To numSlides - 1
visited(i) = False
Next i
qAnswered = False

Sub RandomNext() 'Set a button on all the game pages to this code so you
can jump to another random slide in the game.
Dim nextSlide As Long

If numRead >= numWanted Or numRead >= numSlides - 2 Then
ActivePresentation.SlideShowWindow.View.Last
Else
nextSlide = Int((numSlides - 2) * Rnd + 2)
While visited(nextSlide) = True
nextSlide = Int((numSlides - 2) * Rnd + 2)
Wend
ActivePresentation.SlideShowWindow.View.GotoSlide (nextSlide)
End If
End Sub

If you don't want to go back to a slide already visited then use this code
(along with everything above) and assign the buttons on your game pages to it
to jump to another random slide.

Sub NextRandomSlide()
qAnswered = False
visited(ActivePresentation.SlideShowWindow.View.Slide.SlideIndex) = True
numRead = numRead + 1
RandomNext
End Sub
 
D

David Marcovitz

Valerie,

I was out of town and didn't see this until today. I hope you figured it
out. I see a couple of problems off the top of my head. If you need more
help, let me know, and I can try to help you some more.

(1) GotoSlide 6 should be GotoSlide 27 (it is going to the last slide; 6 was
the last slide in my example.

(2) The line with If doneslide is checking whether all the slides have been
visited, but you are only checking if four of the slides have been visited.

Note that this code was originally written with a small number of slides so
some loops might be in order when you have 26 slides.

Please let me know if you need more help.

--David

I am a teacher created a game for my students I plan to use next week....

I found a post from David and am trying to use it but it is not working. I
want to randomize slides 2-26 in my presentation. So I created a button on
the first page that links to the GetStarted macro and a button on the 2nd
page that links to the GoToRandom macro.

When I added it, it automatically created 3 macros for me (the 3rd being
Initialize). Can you tell me if there is a mistake in my code (see below)?

THANKS!!!!!!!!!!!!!!!!!
Valerie

Sub GetStarted()
Dim doneSlide(6) As Boolean

Sub GetStarted()
Initialize
GoToRandom
End Sub

Sub Initialize()
Dim i As Long

For i = 2 To 26
doneSlide(i) = False
Next i
End Sub

Sub GoToRandom()
Dim nextSlide As Long

Randomize
If doneSlide(2) And doneSlide(3) And doneSlide(26) And doneSlide(5) Then
ActivePresentation.SlideShowWindow.View.GotoSlide 6
Else
nextSlide = Int(Rnd * 26) + 2
While doneSlide(nextSlide)
nextSlide = Int(Rnd * 26) + 2
Wend
doneSlide(nextSlide) = True
ActivePresentation.SlideShowWindow.View.GotoSlide nextSlide
End If
End Sub

Just set the button on your first slide to GetStarted and a button on slides
2, 3, 4, and 5 to GoToRandom
End Sub

--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top