#1   Report Post  
Jeff
 
Posts: n/a
Default Running a VBA Macro

Hello,

It is possible to run a VBA macro automatically when I open a workbook
instead of opening the workbook and then run the VBA macro from the menu
"Tools" "Macro?"
Thanks,

  #2   Report Post  
Peter Rooney
 
Posts: n/a
Default

Jeff,

In the Code window of "ThisWorkbook" in the VBA Editor (Click on
"ThisWorkbook" and press F7), paste the following (for example)

Private Sub Workbook_Open()
'MsgBox ("Good Morning - I'm Open!")
CreateObject("WScript.Shell").Popup "Hello!", 1, " Hello for 1 second! "
End Sub

To run a macro before the workbook closes, try:

Private Sub Workbook_BeforeClose(cancel As Boolean)
'MsgBox ("Leaving Workbook - Goodbye!")
End Sub

And to run code when a particul;ar worksheet is opened or closed, try
pasting these into their code windows in the same way as shown above, by
clicking on the worksheet name and pressing F7

None of these will work if they're in a normal module - they have to be in
the module for the workbook or the appropriate worksheet.

Private Sub Worksheet_Activate()
MsgBox ("Activating Sheet 1!")
End Sub

Private Sub Worksheet_Deactivate()
MsgBox ("Leaving Sheet 1!")
End Sub

Hope these help

Pete


The Createobject is nothing to do with this, it's a handy way of puting a
wait into a macro - the '1' is the number of seconds for which the delay
lasts.




"Jeff" wrote:

Hello,

It is possible to run a VBA macro automatically when I open a workbook
instead of opening the workbook and then run the VBA macro from the menu
"Tools" "Macro?"
Thanks,

  #3   Report Post  
Jason Morin
 
Posts: n/a
Default

Use the Workbook_Open event:

Sub Workbook_Open()
MyMacro
End Sub

where MyMacro is the name of the macro you wish to run.
To use, press ALT+F11 and place the macro
in "ThisWorkbook" module.

HTH
Jason
Atlanta, GA

-----Original Message-----
Hello,

It is possible to run a VBA macro automatically when I

open a workbook
instead of opening the workbook and then run the VBA

macro from the menu
"Tools" "Macro?"
Thanks,

.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running a macro at a certain time Tam Excel Discussion (Misc queries) 3 March 2nd 05 02:50 PM
running macro at automatic time intervals tjb Excel Worksheet Functions 1 February 28th 05 09:40 PM
Shadows of the Pivottable Field List while macro is running David P Excel Worksheet Functions 0 February 25th 05 07:11 PM
Running VBA Macro Jeff Excel Discussion (Misc queries) 1 February 15th 05 01:12 PM
running totals in a macro for each month idunnobutheregoes Excel Worksheet Functions 1 November 8th 04 10:35 PM


All times are GMT +1. The time now is 12:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"