Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
How long do variables retain their values
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? |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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? |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
How long do variables retain their values
ahh...interesting. Thanks for your help!
"Jim Thomlinson" wrote: 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? |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Selecting Rows using Long Variables | Excel Discussion (Misc queries) | |||
VBA - Delete Drop Down, Retain Values | Excel Programming | |||
Add two cell values, retain it, and add another value | Excel Worksheet Functions | |||
Retain values of combo box | Excel Programming | |||
Declaring variables (Long, String, Integer) and interpretation spe | Excel Programming |