How to run a macro by opening a file?

E

Eric

Does anyone have any suggestions on how to run a macro when I open a file?
For example, I would like to run a specific macro within file.xls, if
current time is between 9 am and 10 am, otherwise, don't run this macro.
Does anyone have any suggestions on how to do it? I am using XP with SP3 and
Office 2003.
Thanks in advance for any suggestions
Eric
 
F

FSt1

hi
something like this might work........
Private Sub Workbook_Open()
If Time > "9:00 AM" And Time < "10:00 AM" Then
call ASpecificMacro
Else
MsgBox "current time out of range. terminating macro run."
end if
end sub

this would go in the workbook module.

regards
FSt1
 
E

Eric

Thank everyone very much for suggestions
I would like to trigger a macro by opening a file.xls, but in your coding, I
require to run Workbook_Open macro after a file.xls is opened.
Does anyone have any suggestions on how to trigger macro automatically by
opening file.xls, where the macro is inside the file.xls.
Does anyone have any suggestions?
Thanks in advance for any suggestions
Eric
 
F

FSt1

hi
i think you are confused. workbook_open is an event. the macro will run
automaticly when the workbook is opened.

regards
FSt1
 
O

ozgrid.com

Hi Eric

Private Sub Workbook_Open()
will automatically run opening a Workbook. Macros must be enabled of course.
The code shown previously must reside in the ThisWorkbook Module.
 
F

FSt1

hi
afterthought
the macro would go in the file that you want the code to run at workbook open.
goes in the thisWorkbook module.

Regards
FSt1
 
M

Mike

Eric said:
Thank everyone very much for suggestions
I would like to trigger a macro by opening a file.xls, but in your coding,
I
require to run Workbook_Open macro after a file.xls is opened.
Does anyone have any suggestions on how to trigger macro automatically by
opening file.xls, where the macro is inside the file.xls.
Does anyone have any suggestions?
Thanks in advance for any suggestions
Eric

You could use this. It is automatically run with the workbook is opened.
You may also have to set this if you are opening the workbook from another
workbook macro... ActiveWorkbook.RunAutoMacros xlAutoOpen

Sub Auto_Open()
code here


End Sub

Mike
 
E

Eric

Thank everyone very much for suggestions
When I tried it on a blank worksheet, it works, but when I insert following
codes into my existing file.xls, it cannot run at all. When I open file.xls,
it will request confirmation to activate macro and to update all links, after
that, it stops without performing anything.
Do you have any suggestions what cause it not running?
Thanks in advance for any suggestions
Eric
 
E

Eric

Thank everyone very much for suggestions
When I insert following codes into my existing file.xls, it cannot run at
all. When I open file.xls, it will request confirmation to activate macro and
to update all links, after that, it stops without performing anything.
Do you have any suggestions what cause it not running?
Thanks in advance for any suggestions
Eric

Sub Auto_Open()
nameofmacro
End Sub
 
M

Mike

Eric said:
Thank everyone very much for suggestions
When I insert following codes into my existing file.xls, it cannot run at
all. When I open file.xls, it will request confirmation to activate macro
and
to update all links, after that, it stops without performing anything.
Do you have any suggestions what cause it not running?
Thanks in advance for any suggestions
Eric

Sub Auto_Open()
nameofmacro
End Sub

You may need to create a digital signature for yourself and then add it from
the tools menu in vba.
Mike
 

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