Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If file name = 'abc', then run macro1

Status
Not open for further replies.

dalex17

Technical User
Jul 14, 2002
96
US
Hi,

Is it possible to execute a specific macro depending on a file name? In other words, I have created macro1, macro2, and macro3, and would like for macro3 to run every time the file name abc.xls is opened.

Any replies would be much appreciated.

dalex
 


Hi Dalex:

This problem sounds like a job for the Select Case statement.
Code:
    Select Case Trim(Ucase(FileName))
        Case "ABC.XLS"
             Macro3
        Case "DEF.XLS", "GHI.XLS"
             Macro2
        Case Else
             Macro1
    End Select

HTH,
Cassie
 
Hi Cassie,

Thank you very much for the reply. One question:

1)Where would I write this code (i.e. 'This Workbook', personal module, etc...)


Dalex
 
FYI - This is a Personal.xls macro

Dalex
 
dalex17,
It's very good of Cassie to answer this question here, but this is actually the VB6 forum, not the VBA or Office forum. VBA is covered in forum707 and general Office stuff like this is in forum68

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 


Hi Dalex17:

One can place the code in a variety of places, assuming you are programming in VBA for Excel.

If the code is specific to one sheet in your workbook, then place the code on the appropriate code page (eg., Sheet1).

If the code is specific to more than one page, but still is pertinent to the current workbook, place the code in ThisWorkbook.

If the code is to be used from more than one workbook, I am unsure where to place the code. Maybe it needs to go in one of XLS files in C:\Windows\ShellNew.

HTH,
Cassie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top