Automatically run macro when opening file

C

Chris

Hello,
I would like to run a macro automatically when the Excel file opens.
The macro is written, but what do I do from here?

Thank you,
Chris
 
P

pikus

Open the Workbook.
Press "Alt + F11"
Press "Ctrl + R"
There will be a window labeled Project - VBAProject
Inside that window there will be listed any open workbooks.
Look under the workbook you're working with to find "ThisWorkbook" an
double-click it.
Another window will appear with "ThisWorkbook (CODE)" in the titl
bar.
There will be two drop down menus near the top of this window.
Drop both of these down and select "Workbook" from the one on the lef
and "Open" from the one on the right.
The body of the window should read:

Private Sub Workbook_Open()

End Sub

Place the code there.

- Piku
 
K

Kit

Add a SUB procedure titled Auto_Open to your module.
execute your code in that SUB or call your routine as
needed. The next time you open the spreadsheet your code
will automatically run.

Holding down the SHIFT key when opening the spreadsheet
will suppress running the Auto_Open routine for testing.

Sub Auto_Open()

myRoutine

End Sub

Sub myRoutine()

'Do your code here

End Sub
 
C

chris huber

Pikus-
Thanks! That was the simplest task ever thanks to your detailed
instruction. Much appreciated.
 
D

Drlegend

name your macro
auto_open
see example below



sub auto_open
msgbox("Hello New User")
end sub
 
J

J.E. McGimpsey

Note that this method has been deprecated in favor of using the
Workbook_Open event macro, placed in the ThisWorkbook module:

Private Sub Workbook_Open()
MsgBox "Hello New User" 'no parens needed
End Sub

One significant difference: Event macros will run if the workbook is
opened from code. Automacros require an explicit RunAutoMacro
statement.
 
Joined
Aug 5, 2008
Messages
1
Reaction score
0
pikus said:
Open the Workbook.
Press "Alt + F11"
Press "Ctrl + R"
There will be a window labeled Project - VBAProject
Inside that window there will be listed any open workbooks.
Look under the workbook you're working with to find "ThisWorkbook" an
double-click it.
Another window will appear with "ThisWorkbook (CODE)" in the titl
bar.
There will be two drop down menus near the top of this window.
Drop both of these down and select "Workbook" from the one on the lef
and "Open" from the one on the right.
The body of the window should read:

Private Sub Workbook_Open()

End Sub

Place the code there.

- Piku

--
PIKU >>I had the same question. I did the thing you said, but gives me compile error. It says Sub Expected. I am all new to vba excel project. Please help me out. Thanks.
 

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