Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 26th, 2004, 08:39 PM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to alantodd
Default Auto run when loading a spreadsheet

Hi All,

Totally baffled here, I want to execute a routine when a spreadsheet is opened and tried a lot of things like naming the routine AutoExec AutoRun and AutoStart. None work. Am I barking up the wrong tree? MS Help doesn't.

Am I just dreaming that this is how it is done?

TIA.

Alan
 
Old October 27th, 2004, 03:22 AM
Authorized User
 
Join Date: Aug 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Open a spreadsheet" is not technically correct.
1. You can open a workbook (.xls file).
To get a macro to run with this event go to VBA Editor. View menu/Project Explorer. Doubleclick ThisWorkbook. Select the Open event from dropdown bo top right which gives a macro outlinr :-
Code:
Private Sub Workbook_Open()
    ' put code here
End Sub
2. To trap the event of changing from one worksheet to another use the SheetChange event in the same place :-
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
   ' put code here
End Sub



-----------------------
Regards BrianB
Most problems occur from starting at the wrong place.
Use a cup of coffee to make Windows run faster.
It is easy until you know how.
 
Old October 27th, 2004, 03:27 AM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to alantodd
Default

Well said.

 
Old October 27th, 2004, 03:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Alan,

There are at least a couple of ways to go about this.

Firstly you could use an
Code:
Auto_open
subroutine - tantilisingly close to variants you were trying earlier. The syntax is as follows. Place a subroutine in any code module as shown below, the code in the subroutine will automatically be executed when the file is opened. This is the prefered method of executing code in an add-in. If this method is used in an ordinary spreadsheet and you open that spreadsheet programatically (i.e. use Workbooks.Open in VBA code to open it) the Auto_open sub will not run (just put another line of code calling the auto_open sub in below to specifically get round this limitation)

Code:
Sub Auto_open
    ' Code I want to run when this workbook opens
End Sub
Alternatively you could use the Workbook_Open event. To use this method, navigate to the ThisWorkbook code sheet (under Microsoft Excel Objects in the Visual Basic Editor). Immeaditaly above the code window there should be two drop-down boxes side-by-side saying (General) & (Declarations). From the drop-down box saying (General), left-hand one, select the option Workbook. Upon doing this VBA should automatically create a Workbook_Open event subroutine for you, see below. If Excel has created another event subroutine for you (or if you want to make Excel do something before saving, for example) select the required event from the right-hand drop-down box, and hey-presto you should be done. Alternativly you could just paste the following code into the ThisWorbook code sheet. The weakness of this method is that it will not fire if
Code:
Application.EnableEvents
has been set to False.

Code:
Private Sub Workbook_Open()
    ' Code I want to run when this workbook opens
End Sub
HTH,
Maccas
 
Old October 27th, 2004, 03:36 AM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to alantodd
Default

Hi Guys,

Thanks for the suggestions.

My earlier post may seem strange, but when I read the post, it was blank. Weird!

I have just tried the Workbook_Open() method and this appears perfect for our needs. I knew it would be there somewhere, just couldn't find it.

Thanks again.

Alan

 
Old June 27th, 2009, 02:59 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Excellent answer. But it raises another question... why is auto_open the preferred method for an add-in?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Disable the "Shift Key" on macro Auto run ofAlexander Word VBA 1 June 24th, 2007 03:16 AM
Auto run quit not cleat anukagni Access 4 June 22nd, 2007 01:32 AM
Auto Refresh and Auto Delete deontae45 VB.NET 2002/2003 Basics 1 September 29th, 2006 04:53 PM
Auto Run Program echovue Visual Basic 2005 Basics 0 February 13th, 2006 06:31 PM
implementing auto run from usb flash drive arif_1947 VS.NET 2002/2003 0 October 6th, 2004 10:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.