Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Assign variable value across files

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Assign variable value across files

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
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
Assign Sub Name to a Variable Alan Excel Programming 3 October 10th 06 04:20 PM
Assign value to a variable name Gnarlodious Excel Discussion (Misc queries) 2 August 20th 06 07:10 AM
Assign value to variable rroach Excel Discussion (Misc queries) 1 July 13th 05 05:24 AM
assign variable Tim Excel Programming 10 October 1st 04 07:33 PM
Sum Cells and assign to variable Kitty Excel Programming 2 September 16th 04 08:42 PM


All times are GMT +1. The time now is 01:05 PM.

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"