Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Public variables and separate library files

How does one make a variable available to other routines stored in a
separate library-type file? I thought using "Public", but in the
simple example below the variable "temp" is null within the called
subroutine…

Calling file…
Public temp As Integer
Sub Master()
Workbooks.Open ("\Temp\Library1.xls")
temp = 0
Again:
MsgBox ("Befo" & temp)
Application.Run "Library1.xls!TestSub"
MsgBox ("After:" & temp)
temp = temp + 1
GoTo Again
End Sub
File "\Temp\Library1.xls"…
Sub TestSub()
MsgBox ("Within:" & temp)
End Sub
Output produced…
Befo0
Within:
After:0
Befo1
Within:
After:1
Befo2
Within:
After:2
etc.

If I add a "Public" statement at the top of the Library1.xls module, I
get zeros instead of nulls within the called routine. (It works
correctly if I concatenate the two routines into the same file, but
this of course defeats the purpose of having separate libraries of
code).
  #2   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Public variables and separate library files

hi,
not sure exactly what you're up to but you might use a
trick i have used on occasions. dump the variable into a
cell. then you can use the value in other routines. it
some files(not many) i have a dump zone.

-----Original Message-----
How does one make a variable available to other routines

stored in a
separate library-type file? I thought using "Public", but

in the
simple example below the variable "temp" is null within

the called
subroutine.

Calling file.
Public temp As Integer
Sub Master()
Workbooks.Open ("\Temp\Library1.xls")
temp = 0
Again:
MsgBox ("Befo" & temp)
Application.Run "Library1.xls!TestSub"
MsgBox ("After:" & temp)
temp = temp + 1
GoTo Again
End Sub
File "\Temp\Library1.xls".
Sub TestSub()
MsgBox ("Within:" & temp)
End Sub
Output produced.
Befo0
Within:
After:0
Befo1
Within:
After:1
Befo2
Within:
After:2
etc.

If I add a "Public" statement at the top of the

Library1.xls module, I
get zeros instead of nulls within the called routine. (It

works
correctly if I concatenate the two routines into the same

file, but
this of course defeats the purpose of having separate

libraries of
code).
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Public variables and separate library files

You would need to create a reference from the file needing to use the
variable to the file containing the variable. If you do create the
reference, then if you open the file containing the reference, the file
referenced will also open and you can not close the file referenced until
the file with the reference is closed.

An alternative is to have a function in the file containing the variable
return the value of the variable, then call that function using
Application.Run. This would be a one way path, however.

Essentially, variables are local to the workbook in which they are
contained.

--
Regards,
Tom Ogilvy

"Richard" wrote in message
om...
How does one make a variable available to other routines stored in a
separate library-type file? I thought using "Public", but in the
simple example below the variable "temp" is null within the called
subroutine.

Calling file.
Public temp As Integer
Sub Master()
Workbooks.Open ("\Temp\Library1.xls")
temp = 0
Again:
MsgBox ("Befo" & temp)
Application.Run "Library1.xls!TestSub"
MsgBox ("After:" & temp)
temp = temp + 1
GoTo Again
End Sub
File "\Temp\Library1.xls".
Sub TestSub()
MsgBox ("Within:" & temp)
End Sub
Output produced.
Befo0
Within:
After:0
Befo1
Within:
After:1
Befo2
Within:
After:2
etc.

If I add a "Public" statement at the top of the Library1.xls module, I
get zeros instead of nulls within the called routine. (It works
correctly if I concatenate the two routines into the same file, but
this of course defeats the purpose of having separate libraries of
code).



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
public variables johnny Excel Discussion (Misc queries) 7 February 27th 08 03:44 PM
Public variables johnny Excel Discussion (Misc queries) 2 February 24th 08 05:05 AM
Public Variables Jerry McNabb Excel Discussion (Misc queries) 0 February 24th 08 01:26 AM
public but private variables No Name Excel Programming 2 May 19th 04 12:41 PM
Public Variables with UserForms Mark Worthington Excel Programming 3 February 26th 04 12:26 AM


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