Run Macro on open

  • Thread starter Thread starter Michael
  • Start date Start date
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
 
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)
 
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
 
Back
Top