Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 69
Default Getting a constant/variable from another workbook?

I need to get a value (a string constant) from another open workbook when Auto_Open() is run. Let's call them workbook 1 and 2. When workbook 1 opens, and Auto_Open() is run, it should search workbook 2 for a constant/variable and set one of its own variables to that value.

I've seen that this can be done in Word, using the Document.Variables collection, but I find nothing like that in the Workbook model. Is there a way it can be done?

I know I can also save these values in the registry, but if there's a way to get them directly, that would be preferred.

Gustaf
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Getting a constant/variable from another workbook?

Gustaf,

You could save the variable value to a range in workbook2 in code:

ThisWorkbook.Worksheets("Sheet1").Range("A1").Valu e = varVariable

Then in your workbook1

myVarVariable = Workbooks("Workbook2Name.xls").Worksheets("Sheet1" ).Range("A1").Value

HTH,
Bernie
MS Excel MVP


"Gustaf" wrote in message ...
I need to get a value (a string constant) from another open workbook when Auto_Open() is run. Let's
call them workbook 1 and 2. When workbook 1 opens, and Auto_Open() is run, it should search
workbook 2 for a constant/variable and set one of its own variables to that value.

I've seen that this can be done in Word, using the Document.Variables collection, but I find
nothing like that in the Workbook model. Is there a way it can be done?

I know I can also save these values in the registry, but if there's a way to get them directly,
that would be preferred.

Gustaf



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Getting a constant/variable from another workbook?

why not just

ThisWorkbook.Worksheets("Sheet1").Range("A1").Valu e = _
Workbooks("Workbook2Name.xls").Worksheets("Sheet1" ).Range("A1").Value


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Gustaf,

You could save the variable value to a range in workbook2 in code:

ThisWorkbook.Worksheets("Sheet1").Range("A1").Valu e = varVariable

Then in your workbook1

myVarVariable =
Workbooks("Workbook2Name.xls").Worksheets("Sheet1" ).Range("A1").Value

HTH,
Bernie
MS Excel MVP


"Gustaf" wrote in message
...
I need to get a value (a string constant) from another open workbook when
Auto_Open() is run. Let's call them workbook 1 and 2. When workbook 1
opens, and Auto_Open() is run, it should search workbook 2 for a
constant/variable and set one of its own variables to that value.

I've seen that this can be done in Word, using the Document.Variables
collection, but I find nothing like that in the Workbook model. Is there
a way it can be done?

I know I can also save these values in the registry, but if there's a way
to get them directly, that would be preferred.

Gustaf



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Getting a constant/variable from another workbook?

Patrick,

I read 'variable' as meaning a variable as declared in VBA, with the OP wanting to assign it to
another declared variable in another project.

Bernie
MS Excel MVP


"Patrick Molloy" wrote in message
...
why not just

ThisWorkbook.Worksheets("Sheet1").Range("A1").Valu e = _
Workbooks("Workbook2Name.xls").Worksheets("Sheet1" ).Range("A1").Value


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Gustaf,

You could save the variable value to a range in workbook2 in code:

ThisWorkbook.Worksheets("Sheet1").Range("A1").Valu e = varVariable

Then in your workbook1

myVarVariable = Workbooks("Workbook2Name.xls").Worksheets("Sheet1" ).Range("A1").Value

HTH,
Bernie
MS Excel MVP


"Gustaf" wrote in message ...
I need to get a value (a string constant) from another open workbook when Auto_Open() is run.
Let's call them workbook 1 and 2. When workbook 1 opens, and Auto_Open() is run, it should search
workbook 2 for a constant/variable and set one of its own variables to that value.

I've seen that this can be done in Word, using the Document.Variables collection, but I find
nothing like that in the Workbook model. Is there a way it can be done?

I know I can also save these values in the registry, but if there's a way to get them directly,
that would be preferred.

Gustaf





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Getting a constant/variable from another workbook?

If workbook2 is open, you could create a function inside workbook2 that returns
the value of that variable.

Then workbook1 can call that function--either using Application.run or setting a
reference to that workbook2's project.

In a general module workbook2's project:

Option Explicit
Const myVariableNameHere as string = "Hi there"
Function ReturnMyVar() as String 'whatever
returnmyvar = myVariablenamehere
end function

Then in workbook1's project:

Option Explicit
Sub testme()
Dim Wkbk2Var As String
Dim wkbk1 As Workbook
Set wkbk1 = Workbooks("book1.xls")
Wkbk2Var = Application.Run("'" & wkbk1.Name & "'!ReturnMyVar")
MsgBox Wkbk2Var
End Sub




Gustaf wrote:

I need to get a value (a string constant) from another open workbook when Auto_Open() is run. Let's call them workbook 1 and 2. When workbook 1 opens, and Auto_Open() is run, it should search workbook 2 for a constant/variable and set one of its own variables to that value.

I've seen that this can be done in Word, using the Document.Variables collection, but I find nothing like that in the Workbook model. Is there a way it can be done?

I know I can also save these values in the registry, but if there's a way to get them directly, that would be preferred.

Gustaf


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Getting a constant/variable from another workbook?

funny thing the English language init?


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Patrick,

I read 'variable' as meaning a variable as declared in VBA, with the OP
wanting to assign it to another declared variable in another project.

Bernie
MS Excel MVP


"Patrick Molloy" wrote in message
...
why not just

ThisWorkbook.Worksheets("Sheet1").Range("A1").Valu e = _

Workbooks("Workbook2Name.xls").Worksheets("Sheet1" ).Range("A1").Value


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Gustaf,

You could save the variable value to a range in workbook2 in code:

ThisWorkbook.Worksheets("Sheet1").Range("A1").Valu e = varVariable

Then in your workbook1

myVarVariable =
Workbooks("Workbook2Name.xls").Worksheets("Sheet1" ).Range("A1").Value

HTH,
Bernie
MS Excel MVP


"Gustaf" wrote in message
...
I need to get a value (a string constant) from another open workbook
when Auto_Open() is run. Let's call them workbook 1 and 2. When workbook
1 opens, and Auto_Open() is run, it should search workbook 2 for a
constant/variable and set one of its own variables to that value.

I've seen that this can be done in Word, using the Document.Variables
collection, but I find nothing like that in the Workbook model. Is
there a way it can be done?

I know I can also save these values in the registry, but if there's a
way to get them directly, that would be preferred.

Gustaf




  #7   Report Post  
Posted to microsoft.public.excel.programming
jaf jaf is offline
external usenet poster
 
Posts: 300
Default Getting a constant/variable from another workbook?

Hi Gustaf,
If you place the declares at the top of a module they should be visible to any module/form. (public)

Option Explicit
Dim str1 as string, str2 as string, myPI as double...
str1="hello"
str2="Monday"
myPI=3.14285714285714

Sub firstsub()
....
end sub

John



"Gustaf" wrote in message ...
I need to get a value (a string constant) from another open workbook when Auto_Open() is run. Let's call them workbook 1 and 2.
When workbook 1 opens, and Auto_Open() is run, it should search workbook 2 for a constant/variable and set one of its own variables
to that value.

I've seen that this can be done in Word, using the Document.Variables collection, but I find nothing like that in the Workbook
model. Is there a way it can be done?

I know I can also save these values in the registry, but if there's a way to get them directly, that would be preferred.

Gustaf


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
how can we make one constant number to be act as variable Vimlesh Excel Discussion (Misc queries) 3 June 1st 10 11:26 PM
From variable to excel constant Carmen Excel Programming 2 December 6th 07 03:07 AM
How to average one constant # to variable #s? A1:A2 A1:A3 A1:A4 Colfax Excel Worksheet Functions 1 March 2nd 06 02:49 AM
Variable Acting Like a Constant? George Boynton Excel Programming 2 October 14th 05 04:06 PM
How to declare variable as Excel constant? John Wirt[_6_] Excel Programming 2 August 22nd 04 05:41 PM


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