Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 07-10-2012, 09:29 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline Run Code when Workbook opens Windows 7 32bit Run Code when Workbook opens Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

Hi,

You can't put any old code in the ThisWorkbook module and expect it to run when the workbook opens: you have to specifically tell Excel that you want it to run when the workbook opens.

An Excel workbook has a Workbook_Open() event handler: this is the VBA code it will run when the workbook is opened. In the visual basic editor, double click on the ThisWorkbook module in the project explorer. Then, just above the code pane you'll notice two dropdown boxes (see picture). In the left hand one choose workbook. In the right hand one choose Open (you don't actually need to do this because the Workbook_Open() event handler will be automatically chosen for you) - while you're there have a look at some of the other Workbook event handlers which are available because they might be useful for you in the future. Once this is done, a Workbook_Open() event handler will be created for you in the code pane. You can put whatever code you want to run inside it, or you can call your existing procedure from it.
Attached Images
File Type: png ThisWorkbook.png (65.0 KB, 14 views)
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word opens with 2 pgs jmacky Word 4 01-28-2013 09:10 AM
Run Code when Workbook opens #REF! error when opening a workbook that contain a refference to another workbook tanababa Excel 2 06-07-2012 03:11 PM
Range(Cell1,Cell2) Error on another workbook controlling some other workbook? tinfanide Excel Programming 1 02-09-2012 04:08 PM
Run Code when Workbook opens macro to transfer data from one workbook to another workbook virsojour Excel Programming 5 02-01-2011 08:58 PM
Run Code when Workbook opens Select a range in one one workbook while working in other workbook Slow&Steady Excel 1 02-21-2010 03:34 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft