How d'you pass a variable between projects

Josh00820

New Member
Joined
Mar 8, 2009
Messages
11
Hi everyone!
I am trying to pass a variable from a procedure to a called procedure which resides in a different project. It seems that just by making the variable Public it doesn't get transferred to a out of project procedure.
I appreciate any help!

rgds, J
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi everyone!
I am trying to pass a variable from a procedure to a called procedure which resides in a different project. It seems that just by making the variable Public it doesn't get transferred to a out of project procedure.
I appreciate any help!

rgds, J

Try this:
Create two workbooks - I've left them with their default names of Book1 and Book2. In the VB Editor name the Project for Book1 as Phase1 (it's not really necessary to give the default "Project1" a new name but it helps to be able to identify Project1 in Book1.xls from the Project1 in Book2.xls). Insert a new module in Book1 - call it Globals. Enter the following code into the Globals module:

Public Test As String
Sub tester()
Test = "Hello"
End Sub

Save the workbook with it's default name (on the desktop for the purposes of this test) and leave it open.

In book2 insert a new module.
In that module insert the following code:

Sub tester2()
Dim teststring As String

teststring = test & " World!"
End Sub

In Book2, go to Tools|References.
You should see a Reference to the project you renamed as Phase1.
Check the box next to it and OK out.
You should now see a "References" entry in the Project Explorer pane.
Save the workbook and leave it open.

If you now run tester2 in book2 the message box will just say " World!"
That's because the Global variable in book1 has not been set because that subroutine has not been run yet. So back to book1. Run tester. You have now set the Public string variable Test to "Hello". Now when you run tester2, Test is passed to the referenced workbook project (as "Hello") and the Message box should read "Hello World!".
Is that what you wanted?
 
Upvote 0
D..

I did your example i really helped...thanks a lot!. About renaming the project as Phase1 was great I tried bypassing this one and gets like looking for a needle in a haystack....

Appreciated!!!!

Josh
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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