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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 575
Default 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




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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default 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
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
Private Sub - Only Accessible from same Module? pallaver Excel Discussion (Misc queries) 1 July 8th 08 02:57 PM
STATIC VARIABLE NOT AVAILABLE IN ANOTHER MODULE CAPTGNVR Excel Discussion (Misc queries) 2 February 1st 07 07:30 PM
Initialize a Private variable jerry chapman[_2_] Excel Programming 5 February 21st 05 11:41 PM
Call Private Function from ThisWorkbook module Beto[_3_] Excel Programming 3 February 25th 04 01:36 PM
Variable from a sheet module in a class module in XL XP hglamy[_2_] Excel Programming 2 October 14th 03 05:48 PM


All times are GMT +1. The time now is 02:56 PM.

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

About Us

"It's about Microsoft Excel"