Retain Workbook Name throughout Module

R

Rob

Hi,

I use the below to capture the name of the file that the macro was run from,
this is then used in the Subs code before another Sub is called. My
question is how do I retain the Workbook Name so that I can use in another
Sub within the same module.

iFname = Application.ActiveWorkbook.Name

Thanks, Rob
 
J

JLatham

Define iFname in the declarations portion of the code module (above the first
Sub or Function definition). Do not declare it in any Subs or Functions in
the code module:

Option Explicit
Dim iFname As String

Sub MySub1()
iFname = Application.ActiveWorkbook.Name
End Sub

Sub MySub2()
If iFname="" then
MsgBox "You have not run MySub1 yet"
Else
MsgBox iFname
End if
End Sub

If you need it to be 'visible' across several modules, declare it using
Public instead of Dim.
 

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