View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
keepITcool keepITcool is offline
external usenet poster
 
Posts: 2,253
Default Defining a variable within a sub...

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/de...l=/library/en-
us/modcore/html/decontipsfordefiningproceduresinvba.asp


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


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam

aking1987 wrote in message
:


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.