Run Macro on open

M

Michael

Can you help me?? I have a macro in a spreadsheet that I
want to run every time the file is opened, is there a
command that will run the macro on opening the file.

Thanks
Michael
 
B

Bob Phillips

Either call the macro Auto_Open and put it in a general code module, or add
your code to the Workbook_Open event in ThisWorkbook code module.

The former will not be automatically run if you open the workbook from VBA,
the latter will.

--

HTH

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

Peo Sjoblom

One way

Open the file, press Alt + F11, look in the left project pane and find your
file,
double click ThisWorkbook,in the main window from the dropdown select
Workbook
and you automatically will get the first and the last line of the macro.
Between those lines put your code


Private Sub Workbook_Open()
'your code
End Sub


Note that if you have recorded a macro and it looks like

Sub Macro1()
'do this and that
End Sub

do not include the first and last lines of your macro,
just this part [ 'do this and that ]

so in the end it will look like

Private Sub Workbook_Open()
'do this and that
End Sub
 

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