Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA Excel “Set” question

I am trying to use a variable that was declared as “Public” at a Module
level in one workbook, in a second workbook. I know I need to use a
“Set” statement, but so far I have not come up with the correct
combination to make it work. I am relatively new to VBA and would
appreciate any suggestions.

J. Thomas

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA Excel “Set” question

You do not need to use Set to load a public variable, just assign it like so

myVar = "Bob"

However, you cannot reference a variable in one workbook from another book.
One way around this is to create a simple public sub in the workbook with
the variable, and access the variable from there. You then use that macro
like so

Application.Run "FirstBook.xls"!TestVariable"

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"John Thomas" wrote in message
news:UR2Cf.11772$zh2.321@trnddc01...
I am trying to use a variable that was declared as “Public” at a Module
level in one workbook, in a second workbook. I know I need to use a
“Set” statement, but so far I have not come up with the correct
combination to make it work. I am relatively new to VBA and would
appreciate any suggestions.

J. Thomas



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default VBA Excel =?windows-1252?Q?=93Set=94 question?=

In the 2nd workbook create a reference to the 1st workbook (Tools |
References...). Whether you need a Set or not depends on the type of
variable.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article <UR2Cf.11772$zh2.321@trnddc01,
says...
I am trying to use a variable that was declared as =3FPublic=3F at a Module
level in one workbook, in a second workbook. I know I need to use a
=3FSet=3F statement, but so far I have not come up with the correct
combination to make it work. I am relatively new to VBA and would
appreciate any suggestions.

J. Thomas


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default VBA Excel “Set” question

You can't use a public variable in one workbook in another workbook. Public
means public to the project and the project is the workbook. There are no
application level variables. A workaround would be to put the information
in a defined name (manually it would be Insert=Name=Define but it can be
done in code), then get the information that way.

Now that that is said, there are two workarounds. In the second workbook,
you can go into the VBE and in Tools=References you can create a reference
to the first workbook. Once created, you can use public variables and code
in the first workbook as if it were in the second workbook. If you do
create such a reference and save the workbook so it is preserved, then
whenever you open the second workbook, the first workbook will be opened
automatically.

Another approach would be to put a function or functions in the first
workbook which can work with the public variable. then you can call these
function using Application.Run

First workbook in a general module.

Public myvar as Variant

Public Function SetMyVar(arg1 as variant)
On Error goto ErrHandler
myvar = arg1
SetMyVar = -1
Exit Function
ErrHandler:
SetMyVar = 0
End Function

Public Function ReturnMyVar()
ReturnMyVar = myVar
End Function

then in the second workbook

Dim res as Long
res = Application.Run("FirstBook.xls!SetMyVar",21)
if res = 0 then
msgbox "Problems"
exit sub
End if
msgbox "MyVar = " & Application.Run("FirstBook.xls!ReturnMyVar")

--
Regards,
Tom Ogilvy


"John Thomas" wrote in message
news:UR2Cf.11772$zh2.321@trnddc01...
I am trying to use a variable that was declared as “Public” at a Module
level in one workbook, in a second workbook. I know I need to use a
“Set” statement, but so far I have not come up with the correct
combination to make it work. I am relatively new to VBA and would
appreciate any suggestions.

J. Thomas



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default VBA Excel “Set” question

Tom Ogilvy wrote:
You can't use a public variable in one workbook in another workbook.


You can.
Open first workbook with your variable, open second workbook, go to VBE
open Tools/References and add your first workbook to a list of
references of the second workbook.
Of course it is problematic when your try to move such combination to
another computer, but it works.

Another solution is to have functions which set and return value of such
variable and call then using Application.Run

To call function you don't have to reference it.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default VBA Excel “Set” question

If your going to correct me, at least have the courtesy of reading my
answer before you post.

--
Regards,
Tom Ogilvy

"Kris" wrote in message
...
Tom Ogilvy wrote:
You can't use a public variable in one workbook in another workbook.


You can.
Open first workbook with your variable, open second workbook, go to VBE
open Tools/References and add your first workbook to a list of
references of the second workbook.
Of course it is problematic when your try to move such combination to
another computer, but it works.

Another solution is to have functions which set and return value of such
variable and call then using Application.Run

To call function you don't have to reference it.



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
Excel 2007 Macro/VB Question DDE Question MadDog22 Excel Worksheet Functions 1 March 10th 10 01:47 AM
The question is an excel question that I need to figure out howto do in excel. Terry Excel Worksheet Functions 3 January 23rd 06 06:22 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good davegb Excel Programming 1 May 6th 05 06:35 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 27th 05 07:46 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 23 April 23rd 05 09:26 PM


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