Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
By VB Help Private variables a "Variables that are visible only to the
module in which they are declared." I declared a private variable in a Sub in a module, and wanted to refer it in a function (called from the Sub the variable was declared in) in the same module, but I got a run-time error. Do I misunderstand something, or what did I do wrong? Stefi |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Stefi,
declare the variable at the top of the module. Option Explicit Private strVar as string Sub DoSomething() Dim vRet as variant vret = MyFunction() End Sub Function MyFunction() as variant MyFunction = cvar(strVar) End Function Robin Hammond www.enhanceddatasystems.com "Stefi" wrote in message ... By VB Help Private variables a "Variables that are visible only to the module in which they are declared." I declared a private variable in a Sub in a module, and wanted to refer it in a function (called from the Sub the variable was declared in) in the same module, but I got a run-time error. Do I misunderstand something, or what did I do wrong? Stefi |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Stefi,
You need to declare the variable in the module - not within the sub routine. Declare your private variable outside of a subroutine and then it'll work. Good luck. -- www.alignment-systems.com "Stefi" wrote: By VB Help Private variables a "Variables that are visible only to the module in which they are declared." I declared a private variable in a Sub in a module, and wanted to refer it in a function (called from the Sub the variable was declared in) in the same module, but I got a run-time error. Do I misunderstand something, or what did I do wrong? Stefi |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
That was the problem, now it works.
Thanks, Stefi €˛Snake Plissken€¯ ezt Ć*rta: I suggest to declare variables before subs as follows option explicit dim var1, var2 as string dim var3, var5 as integer dim var4 as long etc. sub name_of_sub_1 () var1= "anystring" ....... end sub sub name_of_sub_2() var3= 5 var5= 6 ...... end sub Function name_of_f(...) 'here u can "read" value of variables from subs and perform a function end function |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I suggest to declare variables before subs as follows
option explicit dim var1, var2 as string dim var3, var5 as integer dim var4 as long etc. sub name_of_sub_1 () var1= "anystring" ....... end sub sub name_of_sub_2() var3= 5 var5= 6 ...... end sub Function name_of_f(...) 'here u can "read" value of variables from subs and perform a function end function |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Private Sub - Only Accessible from same Module? | Excel Discussion (Misc queries) | |||
STATIC VARIABLE NOT AVAILABLE IN ANOTHER MODULE | Excel Discussion (Misc queries) | |||
Initialize a Private variable | Excel Programming | |||
Call Private Function from ThisWorkbook module | Excel Programming | |||
Variable from a sheet module in a class module in XL XP | Excel Programming |