PDA

View Full Version : [SOLVED:] Retaining a variable for 2nd or third execution of macro



Ballarat
11-24-2017, 05:12 PM
I have created a slide which shows club photos and sponsors rolling along sides and bottom. and now want to use that slide to do a raffle draw. I have done it successfully with userform but its appearance on the slide is a bit clunky.
the basic process is
click on a text box -has a 0 displayed on the slide.
message box is displayed to enter start number
message box is displayed to enter the last number
textbox now shows winning number - from random generator
operator now clicks again for redraw if not claimed or for second or third prize.

in code below the second (or third time through) results in 1 as the number displayed not a new random generated number as Endvalue = 1. (On the userform I kept the EndValue in a another textbox that was hidden):banghead:
I would also want to set the displayed text back to 0 at the end or between the draw from 1st to subsequent other draws (I will add an End) command button could I do that then?)


Sub UpdateRandomNumber(oSh As Shape)
Dim X As Long
Dim EndValue As Integer
Dim StartValue As Integer
Dim Message, Title, Default
Dim MyValue As Integer
' Start Number
If oSh.TextFrame.TextRange.Text = "0" Then
Message = "Enter Raffle Start Number" ' Set prompt.
Title = "Raffle Start Number" ' Set title.
Default = "1" ' Set default.
StartValue = InputBox(Message, Title, Default)
' End Number
Message = "Enter Raffle End Number" ' Set prompt.
Title = "Raffle End Number" ' Set title.
Default = "12" ' Set default.
EndValue = InputBox(Message, Title, Default)
' As a test Display dialog box at position 100, 100.
MyValue = InputBox(Message, Title, EndValue, 100, 100)
End If
X = EndValue ' How do I get end value to = what was entered on ist time through?
oSh.TextFrame.TextRange.Text = CStr(Random(X))
SlideShowWindows(1).View.GotoSlide (SlideShowWindows(1).View.Slide.SlideIndex)
' Do it again as a test
SlideShowWindows(1).View.GotoSlide (SlideShowWindows(1).View.Slide.SlideIndex)
oSh.TextFrame.TextRange.Text = CStr(Random(X))
'Now force PPT to go to the slide again (ie, to redraw it) so that
'the changed text appears:
SlideShowWindows(1).View.GotoSlide (SlideShowWindows(1).View.Slide.SlideIndex)
End Sub
Function Random(High As Long) As Long
'Generates a random number less than or equal to
'the value passed in High
Randomize
Random = Int((High * Rnd) + 1)
End Function

Paul_Hossler
11-24-2017, 05:45 PM
1. Welcome to VBX

2. I added CODE tags around your macro to set it off and for formatting - you can use the [#] icon next time

3. It'd probably be easier if you could upload a small PP with enough slide to demonstrate what you want to do

Otherwise people will have to create a PP and make sure shape names, etc. are correct

My sig has instructions

Ballarat
11-25-2017, 03:46 AM
21048

Ballarat
11-25-2017, 03:47 AM
Paul, Thanks for setting me straight on the editing etc

John Wilson
11-25-2017, 07:59 AM
21050This shows how to use Pseudo Event OnSlideShowPageChange to reset variables. NOTE there is a hidden command button on slide 1 top left. This has no code but is essential if the event code is to be reliable.

Ballarat
11-25-2017, 07:21 PM
Thanks John I can complete it now:yes.