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 September 25th, 2003, 06:08 AM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default run code direct from startup

I am looking for a way to start code as soon as a file is opened, without the user having anything to click on etc. Any ideas???

Cheers

 
Old September 25th, 2003, 06:22 AM
Registered User
 
Join Date: Sep 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to RogerWaters
Default

Try This

---------------------------------------------

If you place the code in a module sub called

Sub auto_open()

This will run on opening the spreadsheet.



Roger Waters
 
Old September 25th, 2003, 06:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually you should be using the object event model e.g.

Code:
Private Sub Workbook_Open()
'your code
End Sub
and similarly for close

Code:
Private Sub Workbook_Close()
'your code
End Sub
HTH

Chris

There are two secrets to success in this world:
1. Never tell everything you know
 
Old September 25th, 2003, 06:42 AM
Registered User
 
Join Date: Sep 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to RogerWaters
Default

Hi Chris,

Is the use of events better than the auto_open() ?

Roger Waters
 
Old September 25th, 2003, 08:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, because Auto_xxxx macros don't actually execute if the file (Spreadsheet, word doc etc) is opened/closed via code. If there is an auto_xxxx macro in the file it must be explicity called using something similar to:-

Code:
set wbk = workbooks.open "blah.xls"
wbk.runautomacros xlAutoOpen
On the other hand, events are are always fired, unless explicitly turned off:-

Code:
Application.EnableEvents False
(equivalent to holding down the shift key to prevent auto_open macros from running when open workbooks).


HTH

Chris

There are two secrets to success in this world:
1. Never tell everything you know
 
Old October 18th, 2003, 06:37 AM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is the object event model supported pre xl97? I still have some users on 95 - It's a long story, but I'd like to make my code as standard as possible - I always have to write 2 versions!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Code on Install niall29 Visual Basic 2005 Basics 0 October 17th, 2007 11:14 AM
How to run the sample code rng BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 1 May 4th, 2007 05:18 PM
code run in IE but not in FireFox .... help me ! soi den Dreamweaver (all versions) 1 July 18th, 2006 03:09 AM
Can't Run the Code geotechman BOOK: ASP.NET Website Programming Problem-Design-Solution 1 October 12th, 2004 09:08 PM
Better way to run this code harpua Classic ASP Databases 6 February 27th, 2004 03:41 PM





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