variable in vba powerpoint

  • Thread starter Thread starter chris
  • Start date Start date
C

chris

how can I declare a variable so that I can use it in two
macros that are on two different slides.
example :

ON SLIDE 1 I have a button which runs a macro with the
following code :

Dim grade as integer

grade = 0
grade = grade + 20

On slide two I also have the line
grade = grade + 20 and I would like to have the result be
40 and be displayed in a msgbox
 
Bother, now I had to go and buy the book.

Ollie K

David Marcovitz said:
Put the declaration at the beginning of your module, not at the beginning
of your procedure (sub). For example:
Dim grade

Sub Initialize ()
grade = 0
End Sub

Sub RightAnswer ()
grade = grade + 20
End Sub

Sub DisplayScore ()
MsgBox "Your score is " & grade & "."
End Sub

Link a button on your first slide to Initialize. Link the buttons to any
right answers to RightAnswer, and link a button on your last slide to
DisplayScore. As long as the Dim is the first thing in the module, all
procedures (and all slides) will know about it. I have lots of examples
like this in my book Powerful PowerPoint for Educators. It is due to be
released any day and can be ordered from amazon.com or bn.com.
 
This is where a variable's scope comes into play.

In the VBA Help section, look-up ...
{tab} Contents | Visual Basic Conceptual Topics | Declaring Variables

The help topic boils down to "where you declare the variable, defines when
it will be cleared."

Declare within a sub and it will clear at the end of the sub.
Declare outside the sub, in the module and it will be available in all the
subs (within that module), and will clear at the end of the module (file
end).

--
Bill Dilworth, Microsoft PPT MVP
===============
Please spend a few minutes checking vestprog2@
out www.pptfaq.com This link will yahoo.
answer most of our questions, before com
you think to ask them.

Change org to com to defuse anti-spam,
ant-virus, anti-nuisance misdirection.
..
..

David Marcovitz said:
Put the declaration at the beginning of your module, not at the beginning
of your procedure (sub). For example:
Dim grade

Sub Initialize ()
grade = 0
End Sub

Sub RightAnswer ()
grade = grade + 20
End Sub

Sub DisplayScore ()
MsgBox "Your score is " & grade & "."
End Sub

Link a button on your first slide to Initialize. Link the buttons to any
right answers to RightAnswer, and link a button on your last slide to
DisplayScore. As long as the Dim is the first thing in the module, all
procedures (and all slides) will know about it. I have lots of examples
like this in my book Powerful PowerPoint for Educators. It is due to be
released any day and can be ordered from amazon.com or bn.com.
 
To add another option to Bill's description,
Public iGrade as Interger
if declared at beginning of a module will be available across all
modules Private or Public and will only clear when cleared
specifically or when PPT closes.

Brian Reilly, PowerPoint MVP


Declare within a sub and it will clear at the end of the sub.
Declare outside the sub, in the module and it will be available in all
the
subs (within that module), and will clear at the end of the module
(file
end).
 
As long as we're dipping oars :-)

If you declare a variable as static, it won't clear when you exit the module.

Sub ChangeScore()
MsgBox "Your score is now " & CStr(AddToScore(20))
End Sub

Function AddToScore(iIncrement As Integer) As Integer
' Adds passed value to "running total"
' Returns new total to caller

Static iScore As Integer

' provide a way to reset the score mid-run if need be
If iIncrement = 0 Then
iScore = 0
Else ' note that you can pass a negative score as well
iScore = iScore + iIncrement
End If

AddToScore = iScore

End Function
 
Back
Top