Passing a Variable between Workbooks

TukaranXL

New Member
Joined
May 10, 2011
Messages
15
Hi,

I have a number of workbooks - one main file and several templates. In the main file a variable is assigned 'poolID'. Each of the templates is then opened in turn and macros within these templates are run (with Application.Run), however each template uses the same variable. I currently have to put the variable in a cell on the template once it has opened and then reassign the variable in the template based on that cell, then clear the cell contents.

Is there a way to pass the value of the variable which has been assigned in the main file to the variable in the template file in a sort of ByRef or ByVal way?

Sorry if that's not particularly clear - basically I just want poolID in Workbook B to be populated with the value of poolID in Workbook A.

Cheers
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Can you please post your existing code? If I understand the question correctly, you will need to temporarily store the variable into a public variable that is defined outside of the procedure level, or create your second sub to be something like:

Code:
Public Sub WorksheetB(poolIDa)
'code for WorksheetB
End Sub

Public Sub WorksheetA()
'code for WorksheetA
Call WorksheetB(poolIDb)
End Sub
 
Last edited:
Upvote 0
Hi,

I can't post the exact code due to it's nature (work related), but this is a similar example:

Workbook A (main file):

Global poolID as String
Sub mainRoutine()

poolID = Range("A1").Value
Workbooks.Open("C:\Workbook B.xls"), ReadOnly:=True
Application.Run "'Workbook B.xls'!templateRoutine"

End Sub

Workbook B (template):

Sub templateRoutine()

Sheets(poolID).Select

End Sub

Obviously in the above example I could just put in Workbook A
Windows("Workbook B").Activate
Sheets(poolID).Select
and have the same effect but it is more complicated than that and I want to pass the contents of poolID in Workbook A to a variable of the same name in Workbook B.

Cheers.
 
Upvote 0
Define the variable poolID as a Public variable instead of Global:

Code:
Public poolID as String
 
Upvote 0
Define the variable poolID as a Public variable instead of Global:

Code:
Public poolID as String


Would that allow it to be read by code in another workbook? How would I refer to that in the second workbook - do I need to define it in the 2nd workbook as well?

i.e. If I was to do the following:

Workbook A (main file):

Public poolID as String
Sub mainRoutine()

poolID = Range("A1").Value ' Assume Range A1 contains the text "Hello World"
Workbooks.Open("C:\Workbook B.xls"), ReadOnly:=True
Application.Run "'Workbook B.xls'!templateRoutine"

End Sub

Workbook B (template):

Sub templateRoutine()

MsgBox poolID

End Sub

The message box shown by Workbook B is just blank as the variable poolID has no value in Workbook B even though it is defined as Public and assigned a value prior to Workbook B being opened and the macro called?
 
Upvote 0
Why don't you pass the variable value as a parameter (ByRef or ByVal) to the sub(s) you are calling?

Code:
Option Explicit
 
Sub test()
 
Dim poolID As String
 
poolID = "Sheet3"
 
Workbooks.Open ("C:\TestFile.xls")
 
Application.Run "'TestFile.xls'!templateRoutine", poolID
 
End Sub

Code:
Sub templateRoutine(poolID As String)
 
Sheets(poolID).Select
 
End Sub
 
Upvote 0
Why don't you pass the variable value as a parameter (ByRef or ByVal) to the sub(s) you are calling?

Code:
Option Explicit
 
Sub test()
 
Dim poolID As String
 
poolID = "Sheet3"
 
Workbooks.Open ("C:\TestFile.xls")
 
Application.Run "'TestFile.xls'!templateRoutine", poolID
 
End Sub
___________________________________________________________________________-
Code:
Sub templateRoutine(poolID As String)
 
Sheets(poolID).Select
 
End Sub

Would the two code sections (before and after the dividing line) be in their two own respective workbooks? So subtest would be in one workbook macro and templateRoutine would be in another workbook macro?

Thanks
 
Upvote 0
The sub templateRoutine would be in the workbook named TestFile.xls, declared in a standard module.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top