Auto_Open and global variable

V

VBA beginner

I have tried to save the name of the opened file to variable, so that I can
use it in other modules and worksheets. I have the following auto_open sub:


Sub auto_open()
Global filename As Variant
filename = Application.ThisWorkbook.Name

End Sub

This doesn't work, I have the Compile error message: Invalid attribute in
Sub or Function. Auto_open sub in the module, so what is wrong in this code?
Or is there any other way to do this?
 
G

Gary Keramidas

try this, but i usually put all of the public variables in a separate module

Option Explicit
Public filename As String
Sub auto_open()

filename = Application.ThisWorkbook.Name

End Sub
 
G

Gary Keramidas

oh, and don't use filename, use fname or some other variant. filename is valid
property name in vba.
 
V

VBA beginner

Unfortunately I can't make this work. I have this public variable in separate
module and still the value of the fname variable is empty in the other
module.

"Gary Keramidas" kirjoitti:
 
D

Dave Peterson

Did you run the Auto_Open procedure after you made a change to your code?

If no, then run that Auto_Open. Then do some experimentation with your other
procedure.

ps. I hope that this is a learning exercise. You'd really want to use
ThisWorkbook.Name in other procedures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top