Running a Particular macro on opening a particular excel file

R

rajltd

Can anyone help me to write code which will run a particular marco as
soon as i open a particular excel file. For ex. say Test() marco should
run as soon as I open ABC.xls file.

I have a macro Test() in my personal macro workbook. I have another
excel sheet ABC.xls which is empty at the moment. Now I want Test()
macro to run as soon as I open ABC.xls file so that some stuff is
loaded of the Test() marco.


Can I make the above thing happen if I import ABC.mdb to excel with the
same name. My concern is, if the file imports and then pastes all the
content in excel from the access sheet then this might not work.

Thanks again for your help

Raj
 
D

davers

Hi, there are several ways to do this...I think the simplest is to use
the "ThisWorkbook" object to run your code on open. To reference this
for my personal macro workbook looks like this:


Code:
--------------------

Private Sub Workbook_Open()
Application.Run "'Personal.xls'!lockSht"
End Sub
--------------------


I have a sub defined in my personal macro's called lockSht which locks
all the sheets in my workbook. I think everyones personal macro
workbook is called Personal.xls...you should be able to see it in your
project browser....you can copy and paste the above code into the
workbook's "ThisWorkbook" object and change lockSht to the sub from
your Personal.xls you want it to run when opened....I'm not sure if I'm
explaining myself well or not...I tend to ramble. :)

Hope this helps some....

Dave M.
 
J

J.E. McGimpsey

To run a macro on opening a file:

Put this in the ThisWorkbook code module of ABC.xls (right-click on
the workbook title bar, choose View Code, paste the following in the
window that opens, then click the XL icon on the toolbar to return
to XL):

Private Sub Workbook_Open()
Application.Run "'Personal.xls'!Test"
End Sub

This method will not run the macro if ABC.mdb is imported. For that
you could write a specific macro to import the file and run the
macro.
 

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