PDA

View Full Version : [SOLVED:] VBA to select x number of slides from a range?



mortiferum
05-31-2016, 07:27 AM
Hi, Im very new to VBA

Imagine a PPT with 6 slides in total; slides 1,2,3,4,5 and 6

Slides 1 & 6 are in a fixed position they should always been seen first and last respectively.

I would like to use VBA to randomonly select and show 2 slides out of the 4 remaining slides (2,3,4 & 5).

One way I assume (let me know if theres another) that this can be achieved is to firstly randomise / shuffle the slide order for slides (2,3,4 & 5) and then only display the first two slides from this selection?

I've used the following VBA code that I found on the Internet to 'shuffle' slides 2 to 5 but I cant find out how to only then select the first two slides from this selection?

Sub shufflerange()
Dim Iupper As Integer
Dim Ilower As Integer
Dim Ifrom As Integer
Dim Ito As Integer
Dim i As Integer
Iupper = 5
Ilower = 2
If Iupper > ActivePresentation.Slides.Count Or Ilower < 1 Then GoTo err
For i = 1 To 2 * Iupper
Randomize
Ifrom = Int((Iupper - Ilower + 1) * Rnd + Ilower)
Ito = Int((Iupper - Ilower + 1) * Rnd + Ilower)
ActivePresentation.Slides(Ifrom).MoveTo (Ito)
Next i
Exit Sub
err:
MsgBox "Your choices are out of range", vbCritical
End Sub

After using this code (as an example) I 'could' end up with the slides in the following order 1,3,2,5,4 & 6

Is there a way to VBA code so I end up with slides 1,3,2 & 6 being viewed in the slideshow (without slides 5 & 4 being permently deleted).

If I ran / opened the PPt a 2nd time I could end up with slides 1,5,2 & 6 in the slideshow.

Many thanks

Morty

John Wilson
06-02-2016, 11:30 AM
The guy that wrote the original code is pretty smart!

You could adapt it to HIDE the new slides 4 and 5

You would need to make sure that "next time" you unhid the slides previously hidden.


Sub shufflerange()
Dim Iupper As Integer
Dim Ilower As Integer
Dim Ifrom As Integer
Dim Ito As Integer
Dim i As Integer
Iupper = 5
Ilower = 2
If Iupper > ActivePresentation.Slides.Count Or Ilower < 1 Then GoTo err
'unhide previosly hidden slides ###########################
For i = 1 To ActivePresentation.Slides.Count
ActivePresentation.Slides(i).SlideShowTransition.Hidden = False
Next i
'###########################################################
For i = 1 To 2 * Iupper
Randomize
Ifrom = Int((Iupper - Ilower + 1) * Rnd + Ilower)
Ito = Int((Iupper - Ilower + 1) * Rnd + Ilower)
ActivePresentation.Slides(Ifrom).MoveTo (Ito)
Next i
'hide the new 4 and 5
ActivePresentation.Slides.Range(Array(4, 5)).SlideShowTransition.Hidden = True
Exit Sub
err:
MsgBox "Your choices are out of range", vbCritical
End Sub

mortiferum
06-03-2016, 12:35 AM
Hi John

That code works perfectly, thank you so much for your assistance.

Morty