Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default 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   Report Post  
Posted to microsoft.public.excel.programming
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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Rows using Long Variables pallaver Excel Discussion (Misc queries) 2 July 14th 08 07:13 AM
VBA - Delete Drop Down, Retain Values ajocius[_15_] Excel Programming 1 July 31st 05 09:52 AM
Add two cell values, retain it, and add another value Jay Excel Worksheet Functions 7 April 22nd 05 06:24 PM
Retain values of combo box gig Excel Programming 3 March 9th 05 12:24 AM
Declaring variables (Long, String, Integer) and interpretation spe MrT Excel Programming 4 December 12th 04 12:43 PM


All times are GMT +1. The time now is 05:53 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"