Defining a variable within a sub...

  • Thread starter Thread starter aking1987
  • Start date Start date
A

aking1987

I would like define the variable with different balues either way of the
if then statement.

BUT, use these varialbes within another sub.

IE: define the variable within one sub, but use it in another.
 
Either define them at module level or pass them
as arguments to the procedure.



in short:

Dim x As Long

Sub proA()
Dim y As Long
x = 10
y = 1
Call proB
Debug.Print x; y
Call proC(y)
Debug.Print x; y
Call proD(y)
Debug.Print x; y
End Sub

Sub proB()
x = 20
End Sub

Sub proC(z As Long)
'in vba arguments are passed byref unless specified otherwise)
z = z * 2 'y in the calling proc has now changed
x = x * z
End Sub

Sub proD(ByVal z As Integer)
z = -1 'y is NOT changed, as z is Byval
'note you can also mix datatypes
'to a certain extent...
x = x * z
End Sub

in great detail described here.

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/modcore/html/decontipsfordefiningproceduresinvba.asp


understanding arguments and variables is VERY important.
please buy a good VBA book.
 

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

Back
Top