PC Review


Reply
Thread Tools Rate Thread

Assign variable value across files

 
 
Troubled User
Guest
Posts: n/a
 
      18th Apr 2010
I have 2 files that are open, File A and File B. I have two Public variables
VariableA and VariableB in Files A & B respectively. How do I assign/pass
the value between the two. I know I can do this by assigning the value to a
range and then from a range back to the variable, but what is the syntax for
assigning from one to another.

Thanks in advance.
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      18th Apr 2010
You can create a "sending" function in each workbook's project.

In workbook A's project (in a general module):
Option Explicit
Public VariableA As Variant
Function ShareVarA() As Variant
ShareVarA = VariableA
End Function
Sub SomeSubHere()
VariableA = "hi there" 'some sub that initializes that public variable.
End Sub

And in Workbook B's project:
Option Explicit
Sub GetTheValueFromA()
Dim myResult As Variant
Dim wkbkA As Workbook

Set wkbkA = Workbooks("abook1.xls")

myResult = Application.Run("'" & wkbkA.Name & "'!ShareVarA")

MsgBox myResult
End Sub

=========
Another way is to add a reference to workbook B that points at workbook A.

Then you could use that variable just like it's in workbook B.

First, show the project explorer (in the VBE) by hitting ctrl-r
Next, select workbook's A project
Tools|VBAProject Properties
(VBAProject is the default project name and we want to change it something
unique)
On the General tab, type in the new Project Name. (I used WkbkA, but you should
use something nice (based on the workbook name???).)

Then save this workbook.

Now select Workbook B's project
Tools|References|and select that project that you just renamed (workbook A's
project).

Then your code can look as simple as:
Option Explicit
Sub GetTheValueFromA()
MsgBox VariableA
End Sub

(The function (in the first example) doesn't need to exist.)
Hit F4 to see the properties window
Change the




Troubled User wrote:
>
> I have 2 files that are open, File A and File B. I have two Public variables
> VariableA and VariableB in Files A & B respectively. How do I assign/pass
> the value between the two. I know I can do this by assigning the value to a
> range and then from a range back to the variable, but what is the syntax for
> assigning from one to another.
>
> Thanks in advance.


--

Dave Peterson
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Assign JavaScript variable to Jsp Variable Ranjan Microsoft ASP .NET 1 10th Apr 2007 04:35 PM
How to assign the value of the java script variable to a server side variable in C# code behind code? ravindravarman@gmail.com Microsoft Dot NET 1 5th Apr 2007 04:04 PM
How to assign the value of the java script variable to a server side variable in C# code behind code? ravindravarman@gmail.com Microsoft Dot NET 0 5th Apr 2007 07:58 AM
Assign Sub Name to a Variable Alan Microsoft Excel Programming 3 10th Oct 2006 04:20 PM
Assign value to variable rroach Microsoft Excel Misc 1 13th Jul 2005 05:24 AM


Features
 

Advertising
 

Newsgroups
 


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