Start macro on open

S

Stu

How do i get a macro to run when an excel file is opened without having
to click run macro?

Thanks
 
C

Cliff Myers

Add the macro to your workbook_open event.
It will look like this:
Private Sub Workbook_Open()
macroname
End Sub
 
P

philip

Make sure the macro starts with

(this Auto_Open macro can be placed in any VBA module in
your project)

Sub Auto_Open()

' call your macro here or place your code here

End Sub
 
S

Stu

What if i want it to open on just one sheet and start running and not
the whole workbook?
 
B

Bob Phillips

Stu,

Either name the macro Auto_Open and put it in a normal code module (old way)
or put it in Worksheet_Open event code in ThisWorkbook code module (new
way).

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
D

Don Guillett

A good time to learn to use the archives and/or help. Look for auto_open or
workbook_open
 
B

Bob Phillips

Use the Worksheet_Activate event.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
J

jason

Even if the auto_open is the old way I think it may have the edge.

If you have run someone else's bad code on your machine then they may
have left your application as application.enableevents = false, if
this is the case then when you try to run the workbooks_open event it
won't fire.

The EnableEvents property doesn't seem to affect an Auto_open routine.

J
 
B

Bob Phillips

That's not an edge for auto_open, it's just accommodating the possibility of
poor coding.

Bob
 
Top