ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   private variable: same module, other Sub/Function (https://www.excelbanter.com/excel-programming/333604-private-variable-same-module-other-sub-function.html)

Stefi

private variable: same module, other Sub/Function
 
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



Robin Hammond[_2_]

private variable: same module, other Sub/Function
 
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





John.Greenan

private variable: same module, other Sub/Function
 
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



Stefi

private variable: same module, other Sub/Function
 
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


Snake Plissken[_2_]

private variable: same module, other Sub/Function
 
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


All times are GMT +1. The time now is 03:02 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com