Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Variable losing its value when calling new sub


Is there any way to keep a value in a variable when I call a new sub?

I have a the same variable name in the new sub, and I want to carr
over the value that I had in the first sub which is where I set th
value of the variable.

I am sure there is an easy answer to this, but I am just trying t
learn as I go here..... I may be in over my head.... :confused

--
jim3705
-----------------------------------------------------------------------
jim37055's Profile: http://www.excelforum.com/member.php...fo&userid=2778
View this thread: http://www.excelforum.com/showthread.php?threadid=47384

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Variable losing its value when calling new sub

If the variable is declared in each sub, then these are two separate
variables only visible to their respective subs.

You can declare the variable one time as public at the top of the module and
use it in both subs (remove any other declarations)

Public MyVar as Variant

Sub Init_Myvar()
MyVar = int(rnd()*1000+1)
ShowMyVar
End Sub

sub ShowMyVar()
msgbox MyVar
End Sub

---------------------
or you can past the variable in between
--------------------
Sub Init_Myvar()
Dim MyVar as Variant
MyVar = int(rnd()*1000+1)
ShowMyVar MyVar
End Sub

Sub ShowMyVar(someVar as Variant)
msgbox someVar
End Sub

somVar is a place holder for the variable MyVar


--
Regards,
Tom Ogilvy

"jim37055" <jim37055
wrote in message
...

Is there any way to keep a value in a variable when I call a new sub?

I have a the same variable name in the new sub, and I want to carry
over the value that I had in the first sub which is where I set the
value of the variable.

I am sure there is an easy answer to this, but I am just trying to
learn as I go here..... I may be in over my head....


--
jim37055
------------------------------------------------------------------------
jim37055's Profile:

http://www.excelforum.com/member.php...o&userid=27788
View this thread: http://www.excelforum.com/showthread...hreadid=473844



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Variable losing its value when calling new sub

You can pass variables as arguments...

Sub Test()
dim i as integer

i = 100
call DisplayI(i)
i = i + 50
call DisplayI(i)
end sub

sub DisplayI(byval i as Integer)
msgbox "The value of i is " & i
end sub
--
HTH...

Jim Thomlinson


"jim37055" wrote:


Is there any way to keep a value in a variable when I call a new sub?

I have a the same variable name in the new sub, and I want to carry
over the value that I had in the first sub which is where I set the
value of the variable.

I am sure there is an easy answer to this, but I am just trying to
learn as I go here..... I may be in over my head....


--
jim37055
------------------------------------------------------------------------
jim37055's Profile: http://www.excelforum.com/member.php...o&userid=27788
View this thread: http://www.excelforum.com/showthread...hreadid=473844


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
Calling name of Sheet as a Variable? thekeytothedoor Excel Worksheet Functions 1 January 1st 10 08:48 AM
Capturing Decimal Values in Variable - Losing decimals when summin Mark Excel Programming 1 September 14th 05 05:26 PM
Calling a sub with a variable... Gary Phillips[_2_] Excel Programming 1 July 13th 04 10:25 PM
Calling variable file name Michael[_12_] Excel Programming 2 September 16th 03 03:51 PM
saveas calling a variable Douvid Excel Programming 6 July 28th 03 08:46 AM


All times are GMT +1. The time now is 12:18 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"