View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default How long do variables retain their values

Variables will hold their value for so long as the procedure that created
them is still active. (if you need a varaible to retain it's vale even after
the sub has ended then decalr it Static). Specific to your question it
depends whether the varaible is passed ByVal or ByRef.

When passing variables there are 2 ways. ByVal and ByRef. Unless specified
otherwise you will be passing ByRef. Unless you have a good reason to do so
you should be passing ByVal. When you pass byval you are not passing the
actual variable but rather a copy of the variable. The called sub can do
anything to it
it wants without changing the variable that you passed into the procedure.
Give this a try to see what I mean...

public sub test
dim this as integer
dim that as integer

this = 1
that = 2
msgbox this & " - " & that
call test2(this, that)
msgbox this & " - " & that
end sub

sub Test2(byval this as integer, byref that as integer)
this = this + 10
that = that + 20
msgbox this & " - " & that
end sub

--
HTH...

Jim Thomlinson


"comparini3000" wrote:

say I have a variable called "RowNum"
RowNum is being used in sub Add_Sheets. halfway through Add_Sheets the code
calls sub Hide_Rows. there's more code after Add_Sheets calls Hide_Rows.
Hide_Rows also uses a variable called RowNum. In Add_Sheets RowNum = 98, but
in Hide_Rows RowNum=10. When Excel finishes Hide_Rows and returns to
Add_Sheets, will RowNum still be 98?