Reference VBA variables in another workbook

antoine1

New Member
Joined
May 23, 2010
Messages
11
I am trying to use a variable already declared in a workbook module, in a second workbook.

In the first workbook module I write:

Code:
Public StrA As String
 
Sub SetString()
    StrA = "abc"
    MsgBox StrA
End Sub

I can verify in the inspection window that StrA has the correct value after running SetString.
However, when I run the following in the second workbook module:

Code:
Sub ShowStr()
    MsgBox StrA
End Sub

I find StrA is empty. I have tried to qualify StrA with the project and modules names, but I get errors stating that an object is needed.
The project name is "VBAProject" in both modules. Shouldn't the PUBLIC declaration allow me to share the variable ?

Thanks for any help !
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Just explain what you're trying to do - I bet there are better/easier ways than doing things like this.
 
Upvote 0
You would need to set a reference in the workbook doing the calling to the second workbook whose variable is being called (via Tools>References in the VBE).
 
Upvote 0
Thanks to both for the replies.

I have many different spreadsheet reports which download data from SQL procedures. Until now they were all used inside the office and the server ports were closed, so the user and password were hardcoded inside the macros.
Users now want to use them from outside and I would like to require them to enter their user/password, but only once every time they launch Excel. Only the first report would request identification but all the following would use data already acquired.
I understood that having a common Project name and the Public declaration would allow me to share the variables.
Can you suggest other ways ?
Thanks!!
 
Upvote 0
You can set a reference like Richard suggested or you can latebind to the second workbook without the need for a reference. You can use a latebound function that returns a reference or you can obtain a reference with the object datatype and then call all methods...

MsgBox Application.Workbooks("Book1.xls").StrA

This is an example of a function that returns a latebound reference. This would work if called from another workbook...
 
Last edited by a moderator:
Upvote 0
I'll have to study a little more ;)

With your statement, I get an error stating the object does not admit the property or method. I guess this is because StrA is a declared as a String and not as an Object.

I'll try some more and get back to you if I can't solve it. Thank you !
 
Upvote 0
I'm doing something wrong...
In my first workbook I created a Class module: User

Code:
Private pName As String
Public Property Get Name() As String
    Name = pName
End Property
Public Property Let Name(Value As String)
    pName = Value
End Property

and a standard module:

Code:
Public CurUser As User
 
Sub SetUser()
    Dim u As String
    Set CurUser = New User
    CurUser.Name = "ABC"
    MsgBox CurUser.Name
End Sub

As before I get the correct value in the MsgBox, and I can see that CurUser exists and has the value "ABC" after I run this.

Then in a second workbook I try:

Code:
Sub ShowStr()
    MsgBox Application.Workbooks(1).CurUser.Name
    MsgBox Application.Workbooks(1).Name
End Sub

The first MsgBox gives an error: "Object does not admit this method or property"
The second correctly returns the name of the first workbook.

Can you tell what is wrong ?
Thank you !
 
Upvote 0
Couldn't you just store the user's username and password in a central location and reference that?
 
Upvote 0
Hi Norie,
It would be great, but I don't know how to do it... What would the "central location" be? I only know how to use Excel VBA, not .NET or C#...
Thanks.
 
Upvote 0
Just use text files or the registry - seems much easier.
 
Upvote 0

Forum statistics

Threads
1,214,541
Messages
6,120,110
Members
448,945
Latest member
Vmanchoppy

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