Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thanks! Awesome group. I put out a simple question in the access/vba forum that I couldn't find answered on technet or anywhere else on the web and it was answered the same day!!..."

Geography

Where in the world do Tek-Tips members come from?

Excel VBA: Using Public variables and multiple modules

DJWheezyWeez (TechnicalUser)
11 Jul 12 16:29
Hi,

I've been using Dim x as String variables and I've been playing around with Public instead and I'm having a problem making one work.

In my testing, I've gotten this to work starting from a brand new book with two brand new modules:
Module1:
Public testVar As String
Sub testone()
testVar = "testing testing one two"
End Sub

Module2:
Sub testtwo()
ActiveCell.FormulaR1C1 = testVar
End Sub

When I run the testtwo macro, it fills in "testing testing one two" into the currently active cell. My next attempt at playing around is dealing with opening an existing workbook (as part of a much larger macro).

In my testing, again with a brand new workbook and two new modules, this is what I have but it's not working:
Module1:
Public filePath As String
Sub fpone()
filePath = "W:\IT\Zach\Sales Journal\Testing\"
End Sub

Module2:
Sub fptwo()
Workbooks.Open Filename:=filePath & "Files\Toms_Variance_Setup.xlsm"
End Sub

This time, when I run the fptwo macro, I get an error saying it can't find the 'Files\Toms_Variance_Setup.xlsm' file since it's not using the filePath variable string in the file path. the fptwo macro DOES work if I put the filePath variable inside it instead of fpone.

I'm wondering why does this work in my first example but not the second?
Will this work with some tweaking or is this just not possible?

-DJWW
SkipVought (Programmer)
11 Jul 12 16:46
hi,

Don't know why this would happen.

I used YOUR CODE, substituting a suitable path and workbook name.

Ran fpone
Ran fptwo, which opend my wb.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

DJWheezyWeez (TechnicalUser)
11 Jul 12 16:54
Very odd... I tried it again quickly (as my day is ending) and still wasn't able to get it to run. I thought maybe it was the .xlsm extension and tried a .xls file with no luck. Might it be the version of Excel? I'm using 2010.

I can play around with other shorter file paths or a path on C: instead of W:. Obviously something must be different if I can't run it but you can.
SkipVought (Programmer)
11 Jul 12 16:58

what happens if you add this statement

CODE

Sub fptwo()
fpone
Workbooks.Open Filename:=filePath & "Files\Toms_Variance_Setup.xlsm"
End Sub 

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

DJWheezyWeez (TechnicalUser)
12 Jul 12 9:02
That worked for me. I'm still not sure why it didn't work before but it does now. Thanks :)
SkipVought (Programmer)
12 Jul 12 9:03
Had you actuall run fpone before you ran fptwo?

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

Mrall (Programmer)
28 Jul 12 12:15
I have had problems with public variables working in more than one module for some reason. So as an altertive I store them in a cell in a hidden sheet. It works always.
SkipVought (Programmer)
28 Jul 12 12:35
There is no reason why public variables would not work, unless your method is improper!

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

combo (TechnicalUser)
29 Jul 12 10:40
Sometimes the vba running project is reset, in the vbe options ("general" tab) you can tick "Notify Before State Loss" to be warned in this case. It's a local setting and can be used for testing. You can also use the "Watch" window to see what is going on.

combo

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close