Page 205 - Excel Data Analysis
P. 205
10 537547 Ch09.qxd 3/4/03 12:13 PM Page 191
AUTOMATING ACTIONS WITH MACROS 9
You can use the Open method of the Workbooks collection object
to specify a workbook that Excel should open whenever the current
workbook opens. For example, if your workbook relies on data
values within another workbook, you can open that workbook
whenever the current workbook opens. For example, the following
code opens a workbook called Expenses.xls as read-only.
Example:
Workbooks.Open Filename:="Expenses.xls", ReadOnly:=True
You can use the Object list box in the Code window to quickly
create your Workbook_Open subroutine. The Object list box lists
the available objects for which you can create subroutines within
the current code module. For example, if you access the
ThisWorkbook code module, the only available object is
Workbook.
When you select the Workbook object from the Object list, the
Visual Basic Editor automatically creates a private subroutine
called Workbook_Open. This is because the default event for the
Workbook object is the Open event. If you view the Procedure list
box, you see a list of all available events for the Workbook object.
If you select another event from the list, the Visual Basic Editor
creates a new subroutine for that event.
■ The Visual Basic Editor ˇ Click the Save button to ■ The Workbook_Open
creates a new Private save the workbook including procedure executes the
subroutine named the new subroutine. specified VBA code as
Workbook_Open. the workbook opens.
Á Close Excel.
› Type the VBA code to run
when the workbook opens. ‡ Open the workbook in
Excel.
191