ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Defining a variable within a sub... (https://www.excelbanter.com/excel-programming/316444-defining-variable-within-sub.html)

aking1987[_3_]

Defining a variable within a sub...
 

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.


--
aking1987
------------------------------------------------------------------------
aking1987's Profile: http://www.excelforum.com/member.php...o&userid=15393
View this thread: http://www.excelforum.com/showthread...hreadid=277313


keepITcool

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.





All times are GMT +1. The time now is 12:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com