Excel (VBA) - Collection

Card Puncher Data Processing




Return an object by index

The Item property of a collection returns a single object from that collection.

Set FirstBook = Workbooks.Item(1)

The Item property is the default property for most collections, so you can write the same statement more concisely by omitting the Item keyword.

Set FirstBook = Workbooks(1)

Some collections have predefined index values you can use to return single objects. Each predefined index value is represented by a constant. For example, you specify an XlBordersIndex constant with the Item property of the Borders collection to return a single border.

Worksheets("Sheet1").Range("A1:A1"). _ 
 Borders.Item(xlEdgeBottom).LineStyle = xlDouble


Return an object by name

Before you can use a name with the Item property, you must name the object. Most often, this is done by setting the object's Name property.

ActiveWorkbook.Worksheets.Add.Name = "A New Sheet" 
With Worksheets("A New Sheet") 
 .Range("A5:A10").Formula = "=RAND()" 
End With

Discover More
Card Puncher Data Processing
Excel - Workbook

Workbooks is a collection of all the Workbook objects that are currently open in the Microsoft Excel application. Below are some methods. See Workbooks...

Share this page:
Follow us:
Task Runner