Wednesday, November 2, 2011

Creating a Workbook

A workbook is an object of type Workbook and it is part of the Workbooks collection. To support the ability to create a new

workbook, the Workbooks collection is equipped with a method named Add. Its syntax is:

Workbooks.Add(Template) As WorkbookYou start with the Workbooks class, a period, and the Add method. This method takes only

one argument but the argument is optional. This means that you can call the method without an argument and without

parentheses. Here is an example:

Private Sub cmdNewWorkbook_Click()
Workbooks.Add
End Sub

When the method is called like this, a new workbook would be created and presented to you. After creating a workbook, you may

want to change some of its characteristics. To prepare for this, notice that the Add() method returns a Workbook object.
Therefore, when creating a workbook, get a reference to it. To do this, assign the called method to a Workbook variable. Here

is an example:

Private Sub cmdNewWorkbook_Click()
Dim SchoolRecords As Workbook

Set SchoolRecords = Workbooks.Add
End Sub

After doing this, you can then use the new variable to change the properties of the workbook.

No comments: